Python+Pyramid+Mako:事件中的上下文、视图中的上下文和模板中的上下文有什么区别?

发布于 2025-01-06 19:12:30 字数 1644 浏览 1 评论 0原文

我一直在努力理解这一点,但无法完全了解有关它的精确文档。我对这个 Python Pyramid+Mako 设置中上下文的不同含义感到非常困惑。

这是一些代码片段(告诉我您是否需要更多上下文):

class Root(object):
    request = None
    def __init__(self, request):
        self.request = request

@events.subscriber(events.BeforeRender)
def add_renderer_globals(event):
    event[u'c'] = event[u'request'].tmpl_context
    print u"add_renderer_globals(): request.tmpl_context={0}".format(event[u'request'].tmpl_context)
    print u"add_renderer_globals(): context={0}".format(event[u'context'])

@view.view_config(route_name='login', request_method='GET', renderer='login.mako')
def login_get(context, request):
    print u"login_get(): context={0}".format(context)
    return {}

[...]
cfg = config.Configurator(root_factory=Root,
        package=MyPKG,
        settings=settings,
        session_factory=pyramid_beaker.session_factory_from_settings(settings),
        )

cfg.add_route(name='login', pattern='/login')

cfg.scan()

在我的 mako 模板中,只是为了举一个例子,我只有:

Mako template context=${context}

所以我会提出一个请求,并得到以下输出从控制台或浏览器:

login_get(): context=<MyPKG.Root object at 0x1523c90>
add_renderer_globals(): request.tmpl_context=<pyramid.request.TemplateContext object at 0x12fbc50>
add_renderer_globals(): context=<MyPKG.Root object at 0x1523c90>
Mako template context=<mako.runtime.Context object at 0x15a4950>

我的问题是:有什么区别,你用它们做什么?我也很困惑为什么在语义上我声明了 root_factory=MyPKG.Root 并且它变成了 context =MyPKG.Root 在我和我的订阅者看来。

感谢您提供任何帮助我理解的提示。

I've been trying hard to understand this, but can't quite put a finger on a precise documentation about it. I am quite confused about the different meaning of context in this Python Pyramid+Mako setup.

Here is some code snippets (tell me if you need more context):

class Root(object):
    request = None
    def __init__(self, request):
        self.request = request

@events.subscriber(events.BeforeRender)
def add_renderer_globals(event):
    event[u'c'] = event[u'request'].tmpl_context
    print u"add_renderer_globals(): request.tmpl_context={0}".format(event[u'request'].tmpl_context)
    print u"add_renderer_globals(): context={0}".format(event[u'context'])

@view.view_config(route_name='login', request_method='GET', renderer='login.mako')
def login_get(context, request):
    print u"login_get(): context={0}".format(context)
    return {}

[...]
cfg = config.Configurator(root_factory=Root,
        package=MyPKG,
        settings=settings,
        session_factory=pyramid_beaker.session_factory_from_settings(settings),
        )

cfg.add_route(name='login', pattern='/login')

cfg.scan()

and in my mako template, just to have an example, I only have:

Mako template context=${context}

So I would make a request and I get the following outputs from console or browser:

login_get(): context=<MyPKG.Root object at 0x1523c90>
add_renderer_globals(): request.tmpl_context=<pyramid.request.TemplateContext object at 0x12fbc50>
add_renderer_globals(): context=<MyPKG.Root object at 0x1523c90>
Mako template context=<mako.runtime.Context object at 0x15a4950>

My question is: What are the differences, and what do you use them for? I'm also confused why semantically, I declared root_factory=MyPKG.Root and it becomes context=MyPKG.Root in my view and my subscriber.

Thanks for any hint to help me understand.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

固执像三岁 2025-01-13 19:12:30

首先,忽略 request.tmpl_context。这只是 request 对象上的一个字典,您可以向其中添加内容,并且通常根本不会在 Pyramid 应用程序中使用。它是 Pylons 合并的继子。

使用 Mako 时有两个 context 对象。第一个 (mako.runtime.Context) 由 Mako 提供:http://docs.makotemplates.org/en/latest/runtime.html#context

Pyramid 通常将遍历上下文 (MyPKG.Root) 公开为模板中的上下文。然而,Mako 已经有一个使用该名称的变量。 :-( 因此,Pyramid 的 context 实际上被命名为 _context

First, ignore request.tmpl_context. This is just a dictionary on the request object that you can add stuff to and is not normally used in Pyramid applications at all. It's a step-child from the Pylons merge.

There are two context objects when using Mako. The first (mako.runtime.Context) is supplied by Mako: http://docs.makotemplates.org/en/latest/runtime.html#context

Pyramid typically exposes the traversal context (MyPKG.Root) as context in your templates. However, Mako already has a variable using that name. :-( Thus, Pyramid's context is actually named _context.

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