我的自定义 404 页面不起作用(金字塔框架)

发布于 2025-01-06 04:15:45 字数 309 浏览 6 评论 0原文

我想在金字塔应用程序中显示我精美的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

迷路的信 2025-01-13 04:15:45

下面是一个示例应用程序,它使用异常视图来捕获 Pyramid 在找不到匹配的视图时引发的 Pyramid.httpexceptions.HTTPNotFound 视图:

from waitress import serve
from pyramid.config import Configurator
from pyramid.response import Response

def hello_world(request):
    return Response('<html><body>Hello world!</body></html>')

def notfound(request):
    return Response('<html><body>Not found!</body></html>')

if __name__ == '__main__':
    config = Configurator()
    config.add_view(hello_world)
    config.add_view(notfound, context='pyramid.httpexceptions.HTTPNotFound')
    app = config.make_wsgi_app()
    serve(app, host='0.0.0.0')

访问“/”将返回“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:

from waitress import serve
from pyramid.config import Configurator
from pyramid.response import Response

def hello_world(request):
    return Response('<html><body>Hello world!</body></html>')

def notfound(request):
    return Response('<html><body>Not found!</body></html>')

if __name__ == '__main__':
    config = Configurator()
    config.add_view(hello_world)
    config.add_view(notfound, context='pyramid.httpexceptions.HTTPNotFound')
    app = config.make_wsgi_app()
    serve(app, host='0.0.0.0')

Visiting '/' will return "Hello world!", visiting "/abc" or "/def" (or anything else that isn't found) will return "Not found!".

月棠 2025-01-13 04:15:45

@chris-mcdonough 写的内容在大多数情况下应该有效。但是,如果您在可调用视图中使用 matchdict,并且希望在没有匹配项时显示自定义 404 页面,请确保引发 HTTPNotFound 异常而不是返回它。否则,您将得到默认的 404 页面。

例子:

from pyramid import httpexceptions

def my_page(self):
    id = self.request.matchdict.get('id', None)
    if not id:
        raise httpexceptions.HTTPNotFound()
    else:
        # do whatever here

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:

from pyramid import httpexceptions

def my_page(self):
    id = self.request.matchdict.get('id', None)
    if not id:
        raise httpexceptions.HTTPNotFound()
    else:
        # do whatever here
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文