金字塔烧杯访问已创建的会话
我正在尝试在 Pyramid 框架中使用金字塔烧杯,它只是不起作用,它创建了会话对象,但我无法使用行访问它们
@view_config(route_name='load_qli', renderer='json')
def load_qli(request):
request.environ['beaker.session']
它给出了以下错误
KeyError
KeyError: 'beaker.session'
我的development.ini文件看起来像这样
# pyramid_beaker settings
session.type = file
session.data_dir = %(here)s/data/sessions/data
session.lock_dir = %(here)s/data/sessions/lock
session.key = customerskey
session.secret = customerssecret
session.cookie_on_exception = true
,init.py像这样
from pyramid.config import Configurator
from sqlalchemy import engine_from_config
from qlipe.models import DBSession
from pyramid_mailer import mailer_factory_from_settings
from pyramid_beaker import session_factory_from_settings
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
engine = engine_from_config(settings, 'sqlalchemy.')
DBSession.configure(bind=engine)
# pyramid_beaker add-on
session_factory = session_factory_from_settings(settings)
config = Configurator(
settings=settings,
session_factory=session_factory
)
我创建了像这样的会议
def my_view(request):
session = request.session
session['name'] = 'Fred Smith'
session.save()
我哪里出错了?
I am trying to use pyramid beaker in Pyramid framework and its just not working it creates the session objects but i cannot access them with the line
@view_config(route_name='load_qli', renderer='json')
def load_qli(request):
request.environ['beaker.session']
It gives the following error
KeyError
KeyError: 'beaker.session'
My development.ini file looks like this
# pyramid_beaker settings
session.type = file
session.data_dir = %(here)s/data/sessions/data
session.lock_dir = %(here)s/data/sessions/lock
session.key = customerskey
session.secret = customerssecret
session.cookie_on_exception = true
and init.py like this
from pyramid.config import Configurator
from sqlalchemy import engine_from_config
from qlipe.models import DBSession
from pyramid_mailer import mailer_factory_from_settings
from pyramid_beaker import session_factory_from_settings
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
engine = engine_from_config(settings, 'sqlalchemy.')
DBSession.configure(bind=engine)
# pyramid_beaker add-on
session_factory = session_factory_from_settings(settings)
config = Configurator(
settings=settings,
session_factory=session_factory
)
I create the session like this
def my_view(request):
session = request.session
session['name'] = 'Fred Smith'
session.save()
Where am i going wrong?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

您应该能够仅使用 include 方式,并且 Pyramid_beaker 包可以从 ini 值初始化自身。
在你的 ini 文件中:
或在主函数的 __init__.py 文件中:
你可以在此处阅读更多信息 http://docs.pylonsproject.org/projects/pyramid_beaker/en/latest/#setup
访问会话的常用方法是通过请求,就像您在my_view:
pyramid_beaker 包使用金字塔会话工厂,它管理会话的方式不是像 beaker 的示例那样通过 request.environment['beaker.session'] 对象。有关更多信息,请阅读 http://docs.pylonsproject.org /projects/pyramid/en/1.3-branch/narr/sessions.html
You should be able to just use the include way and the pyramid_beaker package can initialize itself from the ini values.
in your ini file:
or inside your main function's __init__.py file:
You can read more here http://docs.pylonsproject.org/projects/pyramid_beaker/en/latest/#setup
The usual way to access the session is through the request like you do in my_view:
The pyramid_beaker package use the pyramid session factory and the way it manages the session is not through the request.environement['beaker.session'] object like beaker's example. For more info read http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/sessions.html