无法让 Beaker 会话正常工作(KeyError)
我是 Python 世界的新手,在让会话在我的 Web 框架中运行方面遇到了最危险的时刻。 我尝试让 Beaker 会话与 webpy 框架和 Juno 框架一起使用。 在这两个框架中,当我尝试启动会话时,我总是收到 KeyError 。
这是 webpy 中的错误消息(当我尝试在 Juno 中使用烧杯会话时,它几乎完全相同)...
错误
<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']
代码
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()
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()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您还没有创建会话对象,因此您无法在环境中找到它(
KeyError
只是意味着“beaker.session
不在这个字典中”) 。请注意,我不太了解 webpy 和 beaker,所以我无法给您更深入的建议,但根据我从文档和源代码中了解到的内容,这应该可以帮助您入门:
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: