Note that there are some explanatory texts on larger screens.

plurals
  1. POPython+Pyramid+Mako: What is the difference between the context in event, context in view and context in template?
    text
    copied!<p>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.</p> <p>Here is some code snippets (tell me if you need more <i>context</i>):</p> <pre><code>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() </code></pre> <p>and in my mako template, just to have an example, I only have:</p> <pre><code>Mako template context=${context}</code></pre> <p>So I would make a request and I get the following outputs from console or browser:</p> <pre><code>login_get(): context=&lt;MyPKG.Root object at 0x1523c90> add_renderer_globals(): request.tmpl_context=&lt;pyramid.request.TemplateContext object at 0x12fbc50> add_renderer_globals(): context=&lt;MyPKG.Root object at 0x1523c90> Mako template context=&lt;mako.runtime.Context object at 0x15a4950> </code></pre> <p>My question is: What are the differences, and what do you use them for? I'm also confused why semantically, I declared <b><code>root_factory</code></b><code>=MyPKG.Root</code> and it becomes <b><code>context</code></b><code>=MyPKG.Root</code> in my view and my subscriber.</p> <p>Thanks for any hint to help me understand.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload