web2py

How to make api call that requires login in web2py?

随声附和 提交于 2019-12-06 22:48:37
I want to access APIs from application. Those APIs has decorator @auth.requires_login() . I am calling api from controller using demo_app/controllers/plugin_task/task url = request.env.http_origin + URL('api', 'bind_task') page = urllib2.Request(url) page.add_header('cookie', request.env.http_cookie) response = urllib2.urlopen(page) Demo API api.py @auth.requires_login() @request.restful() def bind_task(): response.view = 'generic.json' return dict(GET=_bind_task) def _bind_task(**get_params): return json.dumps({'status': '200'}) Above code gives me error : HTTPError: HTTP Error 401:

Setting the default of a database Field to the value of another field in web2py

耗尽温柔 提交于 2019-12-06 16:01:50
问题 db.define_table('mytable', Field('start_number', 'double', requires=IS_NOT_EMPTY()), Field('current_number', 'double', default=mytable.start_number, requires=IS_NOT_EMPTY())) When something happens on the website, current_number can be changed to something else, but will always start with the default value of start_number (ie, when the entry is created). Each entry has a different start_number . I keep getting an error saying that mytable can't be found, probably because it hasn't been fully

Web2py: Pass a variable from view to controller

橙三吉。 提交于 2019-12-06 14:47:50
问题 How can I pass a JavaScript variable from the view to Python in controller? I don't want to use request.vars or request.args , because that would allow to user to give arbitrary parameters for the action by typing it into the address bar. Example view: <button onclick='myFunction();'>Execute</button> <script> function myFunction();{ var value = calculateValue(); //pass value to my_action window.location = 'my_action'; } </script> Example controller: def index(): return dict() def my_action():

web2py 操作cookie 深入思考

烂漫一生 提交于 2019-12-06 14:07:54
前些日子,用web2py做一个自我日常管理的网站,发现cookie这关死活过不了,思路很简单: 从request获取cookie 然后读取值(没有该cookie则创建) 修改cookie的值,送到response中,这样完成了一次更新 就是这么简单的一个问题,却出现各种奇怪的问题,为难了一段时间,开始觉得网上的API说明太少了,官方的例子也特别的稀少,如今做android和网站交互的时候,只能打开了web2py的服务器来测试android客户端,一时灵感,想解决这个问题,以下是说明和解决方案。 直接调用 response.cookies['your key'] 这样获取值,总是会有问题,但是关注官网的例子,就会发现其实是esponse.cookies['your key'] .value 那么cookies['key']到底得到了什么,为什么操作不正确呢,做了一个测试,直接return这个 response.cookies['your key'] 其结果是这样的 这是什么,web2py框架自动的给这个返回值加了默认布局器,所以完全可以认为返回的是dict对象,不然web2py不会作这一步处理,如果不知道默认布局是什么看起来会非常费劲,因此我对这个值进行遍历,代码如下: {{=len(result)}} <br> {{num=1}} {{ for i in result:}} {{

web2py - how to inject html

橙三吉。 提交于 2019-12-06 10:17:35
问题 i used rows.xml() to generate html output. i want to know how to add html codes to this generated html page e.g: "add logo, link css file,.. etc" rows=db(db.member.membership_id==request.args[0]).select(db.member.membership_id ,db.member.first_name,db.member.middle_name ,db.member.last_name) return rows.xml() 回答1: There are many HTML helpers you can use, for example: html_code = A('<click>', rows.xml(), _href='http://mylink') html_code = B('Results:', rows.xml(), _class='results', _id=1) html

HTML form passing values to web2py

时光总嘲笑我的痴心妄想 提交于 2019-12-06 07:40:55
I am currently working on a page where a user would select all the appropriate fields in a search form (html) which would than pass the values to the web2py query. I am having an issue with writing this query? I am wondering how to actually pass the values from HTML form to the web2py query to be run against the database? When you submit a form to web2py (via GET or POST), all the form variables will be available in request.vars (for more on this, see the book sections on Dispatching and the request object as well as the chapter on forms ). So, you could do something like: def search(): rows =

Processing ember.js tagged html with python

こ雲淡風輕ζ 提交于 2019-12-05 20:31:50
I have the following scenario: We are using web2py in the server side We are serving some ember.js pages Currently those ember.js pages are inside an iframe, because ember.js and web2py conflict with template {{ }} marks . That means we can not easily mix web2py templates and ember.js templates. So I have implemented the helper class solution: class em(DIV) Now I want to process the original ember-tagged html files, and produce the em-tagged files, integrating ember.js and web2py templating-systems into a cohesive unit. For that I need to change all instances of {{XXX}} in the ember.js files

web2py git integration - localhost & pythonanywhere

心不动则不痛 提交于 2019-12-05 19:01:38
I have absolutely no idea how to integrate Github into web2py. I have web2py installed on a usb and also on pythonanywhere . The web2py overview document chapter3 http://web2py.com/books/default/chapter/29/03/overview says : Git integration The admin app also includes git integration. Python git libraries are required, e.g. pip install gitpython This doesn't mean a thing to me!!? I am just getting into programming and trying to make sure everything is set up properly! Any help would be really appreciated! Many thanks pip install gitpython is a command to type into a command line. That would be

How to pass a JSON object to web2py using JQuery Ajax

橙三吉。 提交于 2019-12-05 18:51:01
I've used this method in .NET to pass data back and forth between client and server using JSON objects (both ways). I really liked the method and am looking to do something similar with web2py. Web2py supports returning json objects and supports jsonrpc. I haven't however been able to make it parse a JSON object. My client call looks like this: var testObject = {}; testObject.value1 = "value1value!"; testObject.value2 = "value2value!"; var DTO = { 'testObject' : testObject }; var data = $.toJSON(DTO); //Using the toJSON plugin by Mark Gibson $.ajax({ type: 'POST', url: '/MyWeb2PyApp

Formatting date in Web2py Python

点点圈 提交于 2019-12-05 16:28:17
I'm looking for a function to format a date in order to get day, month and year. Dates are being stored in my database in the following format 2012-09-26. If your goal is to display on web2py template, so you have to use pure Python to format {{=row.datetime_field.strftime("%d/%m/%Y")}} The above will generate 25/09/2012 Tale a look at Python strftime documentations. If you want to show only the day. {{=row.datetime_field.date}} Also you can set it as a representation for that field db.mytable.datetime_field.represent = lambda value, row: value.strftime("format-here") The representation will