subrequests in pyramid

56 views
Skip to first unread message

Jonathan Vanasco

unread,
Feb 7, 2012, 4:30:51 PM2/7/12
to pylons-discuss
i'm trying to emulate how pylons used subrequests to handle htmlfill
population on errors ( i'm using pyramid_handlers )

in my pylons emulation code, i have this:

def form_reprint( request, form_print_method , **htmlfill_kwargs ):
response= form_print_method()

to call it, i would do:
return form_reprint( self.request , self.login_form )

this works as intended if login_form() returns via
"render_to_response()"

however, if login_form users the @action decorator, login_form returns
only a dict.

i'm personally not using the @action decorator much, but I'd like to
get this working nevertheless.

can someone please point me in the right direction? thanks.

Jason

unread,
Feb 7, 2012, 4:54:13 PM2/7/12
to pylons-...@googlegroups.com
You can turn that result dict into the rendered content using pyramid.renderers.render:

return Response(htmlfill.render( render('/template.mako', self.view(), request=self.request) ) )

self.view is an @action method

The only problem with render() is that you have to specify the template name twice; once in the @action call and once in the render call. You also have to remember to pass the request to render().

Michael Merickel

unread,
Feb 7, 2012, 4:57:51 PM2/7/12
to pylons-...@googlegroups.com
Pyramid prior to 1.3 had no way to perform subrequests publicly. The reason is that the renderer attached to a view is only used if that is the active view for a request. Thus, if you have a URL which is mapped to viewA, and you want to delegate the request to viewB, the only way to do this is to call "viewB(request)" and return a Response from viewB (there are obviously other ways to accomplish this similar idea).

So, the decorator/renderer/etc are only active for the primary view each request. It is possible in 1.3 using the new introspection api to get the "active" (or decorated) version of the view and use it.

# find a view that you care about, using the documented properties of a view introspectable
for v in request.registry.introspector.get_category('views'):
    intr = v['introspectable']
    if intr['route_name'] == 'my_route':
        break
else:
    # no view found
    return HTTPNotFound()
view = v['derived_callable']
return view(request.context, request)


Anyway, I apologize for this code, it's also not tested, but that is the basic idea of how you *could* perform a sub-request in pyramid if you wanted the sub-request to go through security checks, and declarative renderers.


--
You received this message because you are subscribed to the Google Groups "pylons-discuss" group.
To post to this group, send email to pylons-...@googlegroups.com.
To unsubscribe from this group, send email to pylons-discus...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/pylons-discuss?hl=en.


Jonathan Vanasco

unread,
Feb 7, 2012, 6:08:25 PM2/7/12
to pylons-discuss
thanks to both.

i don't have a "real" subrequest going on - i'm passing in the name of
the function that I'm calling... so its more like an emulation of a
subrequest.

I think i can get this working. ill post the result.
Reply all
Reply to author
Forward
0 new messages