下面的代码尝试设置会话变量,然后将其读回并将其发送到模板。我想使用webbapp2的框架,而不是GAE-Sessions,以便我完全理解这个过程。
import os, sys, cgi, json, urlparse
sys.path.append("lib")
import jinja2, webapp2, urllib, urllib2
from google.appengine.api import users, oauth, urlfetch
from webapp2_extras import sessions
JINJA_ENVIRONMENT = jinja2.Environment(
loader=jinja2.FileSystemLoader(os.path.dirname(__file__)),
extensions=['jinja2.ext.autoescape'],
autoescape=True)
class BaseHandler(webapp2.RequestHandler):
def dispatch(self):
# Get a session store for this request.
self.session_store = sessions.get_store(request=self.request)
try:
# Dispatch the request.
webapp2.RequestHandler.dispatch(self)
# To set a value:
self.session['foo'] = 'bar'
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.
# Here is where the problem was - the `return` was missing
return self.session_store.get_session()
class CheckVar(BaseHandler):
def get(self):
self.session['foo'] = 'bar'
foo = self.session.get('foo')
context = {
'foo' : foo
}
# Template Settings
temp = 'templates/index.html'
template = JINJA_ENVIRONMENT.get_template(temp)
self.response.write(template.render(context))
config = {}
config['webapp2_extras.sessions'] = {
'secret_key': 'my-super-secret-key',
}
application = webapp2.WSGIApplication([
('/', CheckVar),
], debug=True, config=config)
运行时,会产生以下堆栈跟踪
Traceback (most recent call last):
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1535, in __call__
rv = self.handle_exception(request, response, e)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1529, in __call__
rv = self.router.dispatch(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1278, in default_dispatcher
return route.handler_adapter(request, response)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
File "/Users/bengrunfeld/Desktop/Work/code/mghconsole/main.py", line 22, in dispatch
webapp2.RequestHandler.dispatch(self)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/lib/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
File "/Users/bengrunfeld/Desktop/Work/code/mghconsole/main.py", line 36, in get
self.session['foo'] = 'bar'
TypeError: 'NoneType' object does not support item assignment
我很确定我只是遗漏了一些简单的东西。
答案 0 :(得分:1)
您的session
方法缺少return