开发者

Global variables in web2py controllers

I am want to use global variables in web2py controller but as I have found, controllers is loaded in every request. So my global variable is accessible everywhere but read-only. Changes are not reflected accross sessions/requests.

Is is possible to define globals somewhere? I tried to put global object in controller, i have als开发者_如何学运维o tried to put it to model/db.py.


Try using the database. For example you could create a settings table:

db.define_table('settings,
    Field('max_size', 'integer', default=10),
    Field('summary', 'text', 'abc'),
    ...
)
# create an instance of settings table
settings = db(db.settings.id > 0).select().first()
if not settings:
    settings = db.settings.insert()

And then access it with:

settings.max_size # get the value
settings.update_record(summary=new_summary) # set a value 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜