リクエスト処理にwebapp2を使用し、テンプレートにdjangoを使用するappengineのプロジェクトにwebapp2セッションを追加しました。
このページに続いて、スクリプトに以下を追加しました
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