views:

251

answers:

1

I'm a newb to the Python world and am having the dangest time with getting sessions to work in my web frameworks. I've tried getting Beaker sessions to work with the webpy framework and the Juno framework. And in both frameworks I always get a KeyError when I try to start the session.

Here is the error message in webpy (its pretty much the exact same thing when I try to use beaker sessions in Juno too)...

ERROR

<type 'exceptions.KeyError'> at /
'beaker.session'
Python       /Users/tyler/Dropbox/Code/sites/webpy1/code.py in GET, line 15
Web       GET http://localhost:1234/

15.          session = web.ctx.environ['beaker.session']

CODE

import web
import beaker.session
from beaker.middleware import SessionMiddleware

urls = (
'/', 'index'
)

class index:
    def GET(self):
        session = web.ctx.environ['beaker.session']
        return "hello"


app = web.application(urls, globals())
if __name__ == "__main__": app.run()
+1  A: 

You haven't created the session object yet, so you can't find it in the environment (the KeyError simply means "beaker.session is not in this dictionary").

Note that I don't know either webpy nor beaker very well, so I can't give you deeper advice, but from what I understand from the docs and source this should get you started:

if __name__ == "__main__": app.run(SessionMiddleware)
balpha