@notfound_view_config中描述的Pyramid Docs - Using Hooks使用{{3}}后,我无法获取指定的模板。
views.py:
@notfound_view_config(renderer='templates/notfound.pt')
def notfound(request):
return Response('Not Found, dude', status='404 Not Found')
模板/ notfound.pt:
<html xmlns="http://www.w3.org/1999/xhtml"
metal:use-macro="base">
<tal:block metal:fill-slot="content">
<!-- Example row of columns -->
<div class="row">
<div class="span12">
<h1>Error:</h1>
<p>Uh, oh... you snagged an error:</p>
<pre>"${request}"</pre>
<p>You can return to the <a href="${request.application_url}">homepage</a> if you wish.</p>
</div>
</div>
</tal:block>
</html>
当点击不存在的页面时,我在空白页面上看到“Not Found,dude”消息,但我希望看到我的模板“呃,哦......你遇到了错误!”然后是请求信息。
我怀疑我读错了:
notfound_view_config构造函数接受大部分相同的操作 arguments作为pyramid.view.view_config的构造函数。有可能 在相同的地方使用,并且行为大致相同,除外 它总是注册一个未找到的异常视图而不是'正常' 图。
一方面,似乎我应该能够将'renderer'指定为参数,因为它在pryamid.view.view_config中受支持。另一方面,它听起来总是加载[未找到的异常视图] [3],而不管'渲染器'选项。
真的,我的最终问题(和目标)是,如果找不到某个页面,我该如何显示/渲染我的模板?
答案 0 :(得分:3)
金字塔中的渲染器 - 视图关系始终相同。如果返回Response对象,则会绕过声明的渲染器。这允许您执行if submitted: return HTTPFound(location=...) else: return {}
之类的操作。如果您想影响响应对象并仍然使用渲染器,则返回所需的dict并mutate request.response
,即用于所有渲染器的响应对象。
@notfound_view_config(renderer='templates/notfound.pt')
def notfound(request):
request.response.status = 404
return {}