0
我已閱讀http://webpy.org/cookbook/sessions_with_subapp。 會話工作正常,而我 只有一個應用程序,但是當我創建一個子應用程序,我遇到錯誤〜,下面的詳細信息:與subapp處理web.py會話?
的main.py:
import web
from sub import subapp
web.config.debug = False
urls = ("/sub",subapp,"/", "count",)
app = web.application(urls, globals())
db = web.database(dbn='sqlite', db='./session.db')
store = web.session.DBStore(db, 'sessions')
session = web.session.Session(app, store, initializer={'count': 0})
def session_hook():
web.ctx.session = session
app.add_processor(web.loadhook(session_hook))
class count:
def GET(self):
session.count += 1
return str(session.count)
if __name__ == "__main__":
app.run()
的subapp.py:
import web
urls = ("/", "count",)
subapp = web.application(urls, globals())
session = web.ctx.session
class count:
def GET(self):
session.count += 1
return str(session.count)
錯誤,同時嘗試運行:
Traceback (most recent call last):
File "main.py", line 2, in <module>
from sub import subapp
File "/tmp/session/sub.py", line 9, in <module>
session = web.ctx.session
AttributeError: 'ThreadedDict' object has no attribute 'session'
是的,我只能使用它像web.ctx.session.cout。 – zjj
只能從控制器方法和裝載鉤子中使用'ctx'。 –
thx爲您提供幫助。 – zjj