我的自定义 404 页面不起作用(金字塔框架)
我想在金字塔应用程序中显示我精美的 404 页面,但可以让它工作。在阅读了有关该主题的各种魔术文本后,我在代码中添加了类似的内容:
cfg.add_view( "Page_not_found_view", renderer="page_404.mak",
context=HTTPNotFound )
但是,当调用我的 *Page_not_found_view* 处理程序时(我可以看到它的跟踪),我仍然得到那个可怜的“默认”404 页面,而不是 *我自己的页面page_404.mak*。有什么想法吗?
I want to show my fancy 404 page in pyramid app, but can get it working. After reading various magic texts about the subject, I put something like this in my code:
cfg.add_view( "Page_not_found_view", renderer="page_404.mak",
context=HTTPNotFound )
But while my *Page_not_found_view* handler is invoked (I can see its' trace) I still get that poor "default" 404 page instead of *my own page_404.mak*. Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
下面是一个示例应用程序,它使用异常视图来捕获 Pyramid 在找不到匹配的视图时引发的 Pyramid.httpexceptions.HTTPNotFound 视图:
访问“/”将返回“Hello world!”,访问“/abc”或“/ def”(或任何其他未找到的内容)将返回“未找到!”。
Here's an example app that uses an exception view to catch the pyramid.httpexceptions.HTTPNotFound view raised by Pyramid when no view can be found that matches:
Visiting '/' will return "Hello world!", visiting "/abc" or "/def" (or anything else that isn't found) will return "Not found!".
@chris-mcdonough 写的内容在大多数情况下应该有效。但是,如果您在可调用视图中使用 matchdict,并且希望在没有匹配项时显示自定义 404 页面,请确保引发
HTTPNotFound
异常而不是返回它。否则,您将得到默认的 404 页面。例子:
What @chris-mcdonough wrote should work in most cases. However, if you are using a matchdict in your view callable and want to show your custom 404 page when nothing matches, make sure that you raise the
HTTPNotFound
exception instead of returning it. Otherwise, you will get the default 404 page.Example: