Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/google-app-engine/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用webapp2在Python中的Google应用程序引擎上的会话_Python_Google App Engine_Session_Python 2.7_Webapp2 - Fatal编程技术网

使用webapp2在Python中的Google应用程序引擎上的会话

使用webapp2在Python中的Google应用程序引擎上的会话,python,google-app-engine,session,python-2.7,webapp2,Python,Google App Engine,Session,Python 2.7,Webapp2,下面的代码尝试设置会话变量,然后将其读回并发送到模板。我想使用WebApp2的框架,而不是GAE会话,这样我就能完全理解这个过程 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 se

下面的代码尝试设置会话变量,然后将其读回并发送到模板。我想使用WebApp2的框架,而不是GAE会话,这样我就能完全理解这个过程

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

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

你的
会话
方法缺少了一个
返回

我更新了上面的代码,以便为遇到这个问题的下一个可怜的灵魂提供完整的工作副本。我在我遗漏了一个
返回的那一行上方放了一条评论。谢谢@Greg,你是摇滚明星!要是成为摇滚明星那么容易就好了。