Reputation: 83
I've installed gae-sessions to my development environment but it doesn't seem to be storing anything. Here's a basic example:
session = get_current_session()
session.get('some_num', 3)
Then later on in some other function...
session = get_current_session()
session['some_num'] = 4
And here's the error I get in the console:
KeyError: 'some_num'
Not a very helpful error. I'm pretty sure i've followed the instructions to the letter but maybe there's something i'm missing?
edit
appengine_config.py
from gaesessions import SessionMiddleware
import os
def webapp_add_wsgi_middleware(app):
app = SessionMiddleware(app, os.urandom(32))
return app
Offending code
class Test(webapp.RequestHandler):
def get(self):
session = get_current_session()
if session.is_active():
# set session['some_num'] to whatever was in there or three, otherwise
session['some_num'] = session.get('some_num', 3)
# later, session['some_num'] should exist and be equal to 3 ...
assert session['some_num'] == 3
# ... and is actually 'settable'
session['some_num'] = 4
else:
self.response.out.write("Session is NOT active")
.is_active() doesn't return true.
Upvotes: 0
Views: 683
Reputation: 101149
Looking at the example app (here), it appears that all sessions start off inactive, and are automatically set to active the first time you store a value to them. You should simply be able to store a value to the session with session['foo'] = 'bar'
, and it'll automatically activate the session.
Also note that you shouldn't be generating the cookie key like that. As the docs in the sample appengine_config.py say:
# suggestion: generate your own random key using os.urandom(64)
# WARNING: Make sure you run os.urandom(64) OFFLINE and copy/paste the output to
# this file. If you use os.urandom() to *dynamically* generate your key at
# runtime then any existing sessions will become junk every time you start,
# deploy, or update your app!
Upvotes: 2
Reputation: 188164
In your code (as it stands) you don't add some_num
to your session dictionary in the first place. Try:
from gaesessions import get_current_session
session = get_current_session()
if session.is_active():
# set session['some_num'] to whatever was in there or three, otherwise
session['some_num'] = session.get('some_num', 3)
...
# later, session['some_num'] should exist and be equal to 3 ...
assert session['some_num'] == 3
# ... and is actually 'settable'
session['some_num'] = 4
Upvotes: 0