On Apr 10, 2009, at 2:59 AM, Richard Jones wrote:
Sorry if this is a newbie question, but I'm having trouble figuring out session management in pylons using the built in beaker module.I am attempting to do session management and authentication using the following: class BaseController(WSGIController): requires_auth = False def __before__(self): # set up the session management self = SessionMiddleware(self, key="mykey", secret="randomsecret") # Authentication required? if self.requires_auth and 'user' not in self.session: # Remember where we came from so that the user can be sent there # after a successful login self.session['path_before_login'] = request.path_info self.session.save() return redirect_to(h.url_for(controller='login'))
SessionMiddleware is WSGI middleware, which means it runs in the middleware stack in your projects config/middleware.py. You're not supposed to be setting it up per request on your controller, its already running in the middleware stack, so you can just use the session via the Pylons global.
To use sessions, merely ensure that at the top of your module you have: from pylons import sessionThen just use 'session' in your controller as you did (minus the 'self.' bit). Have you read pylonsbook.com? I believe it goes into this a bit more.
Cheers, Ben
smime.p7s
Description: S/MIME cryptographic signature
