Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Webapp2 sessions are lost between requests

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.

like image 522
Matt Zeunert Avatar asked Dec 11 '12 10:12

Matt Zeunert


1 Answers

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)
like image 186
John Avatar answered Oct 06 '22 22:10

John