4

我在 appengine 上的项目中添加了 webapp2 会话,该项目使用 webapp2 进行请求处理,使用 django 进行模板。

此页面之后,我将以下内容添加到我的脚本中

import webapp2

from webapp2_extras import sessions

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)
        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()

config = {}
config['webapp2_extras.sessions'] = {
    'secret_key': 'my-super-secret-key',
}

但是,现在我收到以下错误

Traceback (most recent call last):
  File "/home/user/google_appengine/lib/webapp2/webapp2.py", line 1536, in __call__
    rv = self.handle_exception(request, response, e)
  File "/home/user/google_appengine/lib/webapp2/webapp2.py", line 1530, in __call__
    rv = self.router.dispatch(request, response)
  File "/home/user/google_appengine/lib/webapp2/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/home/user/google_appengine/lib/webapp2/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/home/user/sites/project2n/main.py", line 804, in dispatch
    self.session_store = sessions.get_store(request=self.request)
  File "/home/user/google_appengine/lib/webapp2/webapp2_extras/sessions.py", line 454, in get_store
    store = request.registry[key] = factory(request)
  File "/home/user/google_appengine/lib/webapp2/webapp2_extras/sessions.py", line 308, in __init__
    required_keys=('secret_key',))
  File "/home/user/google_appengine/lib/webapp2/webapp2.py", line 1361, in load_config
    self._validate_required(key, config, required_keys)
  File "/home/user/google_appengine/lib/webapp2/webapp2.py", line 1375, in _validate_required
    'Missing configuration keys for %r: %r.' % (key, missing))
Exception: Missing configuration keys for 'webapp2_extras.sessions': ['secret_key']

我已经设置了密钥,使用 config. 我没有在会话中使用其他任何东西。其他进口是

import os,sys, string, time, sha, Cookie
from django.template import Context, Template
from django.template.loader import get_template
4

2 回答 2

15

您需要在某处包含此配置,例如在您创建的 WSGI 应用程序中:

app = webapp2.WSGIApplication([('/path', MyHandler)],
                              config=config,
                              debug=True)
于 2012-11-16T17:41:51.693 回答
2

顺序很重要:

config = {}
config['webapp2_extras.sessions'] = {
    'secret_key': 'my-super-secret-key',
}

app = webapp2.WSGIApplication([
    ('/', HomeHandler),
], config=config)

https://webapp-improved.appspot.com/api/webapp2_extras/sessions.html

于 2014-02-11T05:24:35.550 回答