简体   繁体   English

使用webapp2在Python中使用Google App Engine进行的会话

[英]Sessions on Google App Engine in Python using webapp2

The code below attempts to set a session variable, then read it back and send it to a template. 下面的代码尝试设置会话变量,然后将其读回并发送到模板。 I would like to use webbapp2's framework, not GAE-Sessions, so that I fully understand the process. 我想使用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)

When run, it produces the following stack trace 运行时,它将产生以下堆栈跟踪

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

I'm pretty sure that I'm just missing something simple. 我很确定我只是缺少一些简单的东西。

您的session方法缺少return

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM