Python+Pyramid+Mako:事件中的上下文、视图中的上下文和模板中的上下文有什么区别?
我一直在努力理解这一点,但无法完全了解有关它的精确文档。我对这个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先,忽略 request.tmpl_context。这只是
request
对象上的一个字典,您可以向其中添加内容,并且通常根本不会在 Pyramid 应用程序中使用。它是 Pylons 合并的继子。使用 Mako 时有两个
context
对象。第一个 (mako.runtime.Context
) 由 Mako 提供:http://docs.makotemplates.org/en/latest/runtime.html#contextPyramid 通常将遍历上下文 (
MyPKG.Root
) 公开为模板中的上下文
。然而,Mako 已经有一个使用该名称的变量。 :-( 因此,Pyramid 的context
实际上被命名为_context
。First, ignore
request.tmpl_context
. This is just a dictionary on therequest
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#contextPyramid typically exposes the traversal context (
MyPKG.Root
) ascontext
in your templates. However, Mako already has a variable using that name. :-( Thus, Pyramid'scontext
is actually named_context
.