在同一视图方法中在渲染器之间切换的简单方法
我现在像这样设置我的函数
@view_config(
route_name = 'route_name',
permissions = 'permissions',
renderer = 'r.mako'
)
def r( request ):
# stuff goes here
,我想添加功能,以便我检查某些条件(使用ajax)我将使用一个模板,否则使用另一个模板。有没有办法在金字塔中做到这一点?谢谢
I set up my function like this
@view_config(
route_name = 'route_name',
permissions = 'permissions',
renderer = 'r.mako'
)
def r( request ):
# stuff goes here
now, I want to add functionality such that I check certain conditions (using ajax) i would use one template, otherwise use another. is there a way to do this in pyramid? thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您可以通过谓词确定要执行的操作,则可以使用不同的渲染器多次添加视图。例如
,或者您可以通过
request.override_renderer = 'b.mako'
手动覆盖渲染器:http://docs.pylonsproject.org/ items/pyramid/en/1.3-branch/narr/renderers.html#overriding-a-renderer-at-runtime
或者您可以通过显式渲染响应视图中的
render
和render_to_response
方法,因为如果您从以下位置返回Response
对象,则renderer
参数将被忽略的观点。请注意,第一个示例中的 xhr 谓词应该足以检查 ajax 请求。另请注意,如果您不愿意,则不必为两者使用相同的视图,具体取决于情况。
Well you can add the view multiple times with different renderers if you can determine what you want to do via predicates. For example
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
andrender_to_response
methods from within the view, as therenderer
argument is ignored if you return aResponse
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.