Search code examples
pyramid

Pyramid: passing in context in render_view(), but original request.context remains


I am slightly trying to abuse render_view() to dynamically look up viewlets to render inside my main view. However, the framework is robust and my abusing attempts have utterly failed. The problem is specifically: when I call render_view() and manually pass in context, I don't know how to look up this context in the target view.

  • I render view admin()
  • In this view I render admin panel views, for each I pass admin panel as context through render_view()
  • But in render_view(), the original context of admin() remains and I do not know how to read the passed in context

Is it possible to pick up passed in context somewhere? Or is there a better alternative to render subviews (viewlets?) in Pyramid, instead of abusing render_view()

@view_config(route_name='admin', renderer='admin/admin.html', permission='view')
def admin(request):
    # Herer request.context = Admin
    p = AdminPanel()
    rendered_panel = render_view(context=p, name="admin_panel", secure=True, request=request) 
    return dict(panels=rendered_panel)


@view_config(context=AdminPanel, name="admin_panel", renderer='string', permission='view')
def panel(request):
    # XXXXXXXXXXXXX
    # This is the original request.context Admin, not one I passed in render_view() above
    # XXXXXXXXXXXXX
    context = request.context
    ....

Solution

  • Apparently you can have context as the first argument of view function. Somehow I had missed this as there were not too many examples of this sort:

       @view_config(context=AdminPanel, name="admin_panel", renderer='string', permission='view')
       def panel(context, request):