I'm using webapp2_extras to set a session variable. If I print the variable after setting it the value is correct. The save_sessions call is also made. However, when I make the request again the variable isn't set.
Most of the code is taken from the webapp2 documentation:
import webapp2
from webapp2_extras import sessions
class BaseHandler(webapp2.RequestHandler):
def dispatch(self):
self.session_store = sessions.get_store(request=self.request)
try:
webapp2.RequestHandler.dispatch(self)
finally:
self.session_store.save_sessions(self.response)
@webapp2.cached_property
def session(self):
return self.session_store.get_session()
class MainHandler(BaseHandler):
def get(self):
foo = self.session.get('foo')
print foo
self.session['foo'] = 'bar'
foo = self.session.get('foo')
print foo
self.response.write(BaseHandler)
config = {}
config["webapp2_extras.sessions"] = {
"secret_key": "key",
}
app = webapp2.WSGIApplication([
('/', MainHandler)
], debug=True, config=config)
This always returns "None something" but I want it to return "something something" for the second request.
I know this is a fairly old question but I just ran into the same problem and here is what solved it for me.
I altered the webapp2 config object to add in the auth param.
What used to be:
config = {}
config["webapp2_extras.sessions"] = {"secret_key": "key" }
app = webapp2.WSGIApplication([('/', MainHandler)], debug=True, config=config)
Should now be:
config = {}
config["webapp2_extras.sessions"] = {"secret_key": "key" }
config["webapp2_extras.auth"] = {'session_backend': 'securecookie'}
app = webapp2.WSGIApplication([('/', MainHandler)], debug=True, config=config)
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