http://webpy.org/cookbook/sessions_with_subappを読みました。アプリが 1 つしかない場合、セッションは正常に機能しますが、サブアプリを作成すると、エラー ~ が発生します。詳細は以下のとおりです。
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'