【问题标题】:Sessions on Google App Engine in Python using webapp2使用 webapp2 在 Python 中的 Google App Engine 上的会话
【发布时间】:2014-03-08 06:45:47
【问题描述】:

下面的代码尝试设置会话变量,然后将其读回并将其发送到模板。我想使用 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

我很确定我只是缺少一些简单的东西。

【问题讨论】:

  • 我更新了上面的代码,以便为遇到这个问题的下一个可怜的灵魂提供一个完整的工作副本。我在我遗漏了return 的行上方发表了评论。

标签: python google-app-engine session python-2.7 webapp2


【解决方案1】:

您的session 方法缺少return

【讨论】:

  • 谢谢@Greg,你是摇滚明星!
  • 如果成为摇滚明星这么容易就好了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-09-09
  • 2011-10-10
  • 1970-01-01
  • 1970-01-01
  • 2012-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多