Calling another view in Pyramid

前端 未结 6 1418
庸人自扰
庸人自扰 2021-02-02 16:02

My goal: In Pyramid, to call another view-callable, and to get a Response object back without knowing any details about that view-callable.

In my Pyramid a

6条回答
  •  猫巷女王i
    2021-02-02 16:37

    I was struggling with this as well. I have a solution using the render_to_response method, though I'm sure there's a "more correct" way to do it. Until someone posts it, however, here is how I handled this:

    from pyramid.renderers import render_to_response
    
    @view_config(route_name="foo", renderer="foo.mak")
    def foo_view(request):
        return {'stuff':'things', '_renderer':'foo.mak')
    
    def bar_view(request):
        values = foo_view(request)
        renderer = values['_renderer']
        return render_to_response(renderer,values)
    

    (Pyramid 1.3)

    This requires a renderer to be used, but by declaring that renderer in the original view's return values, you can retrieve it in another view without knowing what it is. I'm suspecting the need to do this isn't easily findable because there's other, better methods for accomplishing tasks solved by this solution.

    Another shortcoming is that it relies on direct import of the view callable. It would be nice if it could be looked up directly by route.

提交回复
热议问题