Easy way to switch between renderers within the same view method

冷暖自知 提交于 2019-12-01 11:31:20

Well you can add the view multiple times with different renderers if you can determine what you want to do via predicates. For example

@view_config(route_name='route', xhr=True, renderer='json')
@view_config(route_name='route', renderer='r.mako')
@view_config(route_name='route', request_param='fmt=json', renderer='json')
def r(request):
    # ...

Or you can just override the renderer manually via request.override_renderer = 'b.mako':

http://docs.pylonsproject.org/projects/pyramid/en/1.3-branch/narr/renderers.html#overriding-a-renderer-at-runtime

Or you can just explicitly render the response via the render and render_to_response methods from within the view, as the renderer argument is ignored if you return a Response object from the view.

Note that the xhr predicate in the first example should be sufficient to check for an ajax request. Note also that you don't have to use the same view for both if you don't want to, just depends.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!