我试图实现 GAE 的 webapp2 session ,但似乎没有关于它的文档。根据http://webapp-improved.appspot.com/api/webapp2_extras/sessions.html ,我的步骤如下:
1.配置主应用并添加配置:
config = {}
config['webapp2_extras.sessions'] = {
'secret_key': 'my_secret_key',
}
app = webapp2.WSGIApplication([...], config=config)
2.在登录处理程序中创建 session
# Delete existent session
--> not mention in the tutorial
# member is found
self.session_store = sessions.get_store(request=handler.request)
self.session['account'] = member.account
3.检查 session 是否存在于我程序的不同位置
if self.session['account']:
# Session exists
4.用户注销时删除session
--> not mentioned in the tutorial
我的问题:
我在 session 创建过程(第 2 步)期间收到错误消息“...对象没有属性‘session’”
如何在步骤 2 和 4 中删除 session ?
整个 session 管理流程是否正确?
谢谢。
最佳答案
这是处理程序的示例以及如何使用 webapp2 额外 session
带有 BaseHandler 和 MainHandler 的 main.py
import webapp2
from webapp2_extras import sessions
class BaseHandler(webapp2.RequestHandler): # taken from the webapp2 extrta session example
def dispatch(self): # override dispatch
# Get a session store for this request.
self.session_store = sessions.get_store(request=self.request)
try:
# Dispatch the request.
webapp2.RequestHandler.dispatch(self) # dispatch the main handler
finally:
# Save all sessions.
self.session_store.save_sessions(self.response)
@webapp2.cached_property
def session(self):
# Returns a session using the default cookie key.
return self.session_store.get_session()
class YourMainHandler(BaseHandler):
def get(self):
....
self.session['foo'] = 'bar'
def post(self):
foo = self.session.get('foo')
如果你有一个单独的 login.py :
.... other imports
import main
class Login(main.BaseHandler):
def get(self):
....
self.session['foo'] = 'bar'
def post(self):
foo = self.session.get('foo')
关于google-app-engine - GAE webapp2 session : the correct process of creating and checking sessions,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14078054/