金字塔烧杯访问已创建的会话

发布于 12-27 13:29 字数 1415 浏览 7 评论 0原文

我正在尝试在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

佞臣2025-01-03 13:29:28

您应该能够仅使用 include 方式,并且 Pyramid_beaker 包可以从 ini 值初始化自身。

在你的 ini 文件中:

pyramid_includes = pyramid_beaker

或在主函数的 __init__.py 文件中:

config.include('pyramid_beaker')

你可以在此处阅读更多信息 http://docs.pylonsproject.org/projects/pyramid_beaker/en/latest/#setup

访问会话的常用方法是通过请求,就像您在my_view:

session = request.session

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:

pyramid_includes = pyramid_beaker

or inside your main function's __init__.py file:

config.include('pyramid_beaker')

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:

session = request.session

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文