I added webapp2 sessions to my project on appengine which uses webapp2 for request handling and django for templates.
Following this page, I added the following to my script
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',
}
However, now I get the following error
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']
I have set the secret key, using config. I am not using anything else for sessions. The other imports are
import os,sys, string, time, sha, Cookie
from django.template import Context, Template
from django.template.loader import get_template
You need to include this config somewhere, such as in the WSGI app you create:
app = webapp2.WSGIApplication([('/path', MyHandler)],
config=config,
debug=True)
The order is important:
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With