Python 我的自定义404页面没有';t工作(金字塔框架)

Python 我的自定义404页面没有';t工作(金字塔框架),python,http-status-code-404,pyramid,Python,Http Status Code 404,Pyramid,我想在金字塔应用程序中显示我的404页面,但可以让它工作。在阅读了有关该主题的各种魔法文本后,我在代码中添加了如下内容: cfg.add_view( "Page_not_found_view", renderer="page_404.mak", context=HTTPNotFound ) 但是,虽然调用了我的*Page\u not\u found\u view*处理程序(我可以看到它的“跟踪”),但我仍然得到了糟糕的“default”404页面,而不是*my-

我想在金字塔应用程序中显示我的404页面,但可以让它工作。在阅读了有关该主题的各种魔法文本后,我在代码中添加了如下内容:

cfg.add_view( "Page_not_found_view", renderer="page_404.mak", 
               context=HTTPNotFound )

但是,虽然调用了我的*Page\u not\u found\u view*处理程序(我可以看到它的“跟踪”),但我仍然得到了糟糕的“default”404页面,而不是*my-own Page\u 404.mak*。有什么想法吗?

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

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')
从女服务员进口服务
从pyramid.config导入配置程序
从pyramid.response导入响应
def hello_world(请求):
返回响应('Hello world!')
def未找到(请求):
返回响应('未找到!')
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
config=Configurator()
config.add_视图(hello_world)
config.add_视图(notfound,context='pyramid.httpexceptions.HTTPNotFound')
app=config.make_wsgi_app()
服务(应用程序,主机=0.0.0.0')

访问“/”将返回“Hello world!”,访问“/abc”或“/def”(或任何未找到的内容)将返回“Not found!”。

以下是一个示例应用程序,该应用程序使用异常视图捕获pyramid.httpexceptions.HTTPNotFound视图,当找不到匹配的视图时,pyramid将引发该视图:

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')
从女服务员进口服务
从pyramid.config导入配置程序
从pyramid.response导入响应
def hello_world(请求):
返回响应('Hello world!')
def未找到(请求):
返回响应('未找到!')
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
config=Configurator()
config.add_视图(hello_world)
config.add_视图(notfound,context='pyramid.httpexceptions.HTTPNotFound')
app=config.make_wsgi_app()
服务(应用程序,主机=0.0.0.0')

访问“/”将返回“Hello world!”,访问“/abc”或“/def”(或任何未找到的内容)将返回“Not found!”。

在大多数情况下,@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

在大多数情况下,@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

是的,这个有效。问题仍然是为什么我的代码不起作用?你的回答给了我一个暗示。看起来(renderer=“page_404.mak”)机器不适用于404处理程序。在用“手动”呈现替换代码之后,我的代码开始工作,比如:render_to_response('page_404.mak',{page params here},request=request)是的,这很有效。问题仍然是为什么我的代码不起作用?你的回答给了我一个暗示。看起来(renderer=“page_404.mak”)机器不适用于404处理程序。在用“手动”呈现替换代码之后,我的代码开始工作,比如:render_to_response('page_404.mak',{page params here},request=request),感谢您的重要提示!事实上,我发现我的代码遇到了这两个问题。首先-我的模板尚未生效(通过“renderer”参数指定模板不起作用),然后我没有引发异常,而是返回了异常。感谢您的重要提示!事实上,我发现我的代码遇到了这两个问题。首先-我的模板没有生效(通过“renderer”参数指定模板不起作用),然后我没有引发异常,而是返回了它。应该是.mako而不是.mak吗?我使用.mak,金字塔图书也是如此。。。我想两者都应该是。mako而不是。mak?我用。mak。金字塔书也是。。。我认为两者都可以