Python 如何在金字塔中的HTTPNotFound错误中包含链接?

Python 如何在金字塔中的HTTPNotFound错误中包含链接?,python,http-status-code-404,pyramid,html-escape-characters,Python,Http Status Code 404,Pyramid,Html Escape Characters,我的网站的一部分是wiki引擎。当一个页面不存在时,我想提供一个自定义404错误页面,其中包含创建新页面的链接。此自定义404只能在失败的wiki页面视图的上下文中看到 为了实现这个逻辑,我只需返回(而不是引发)一个HTTPNotFound()对象,该对象带有一条自定义消息,其中包含用于创建新页面的链接。不幸的是,链接被转义了。 如何强制html链接显示为链接 编辑: 我找到了一张解决方案表 很可能这样的对象已经存在于金字塔中金字塔中的未找到视图接受与常规视图相同的谓词 config.add_r

我的网站的一部分是wiki引擎。当一个页面不存在时,我想提供一个自定义404错误页面,其中包含创建新页面的链接。此自定义404只能在失败的wiki页面视图的上下文中看到

为了实现这个逻辑,我只需返回(而不是引发)一个HTTPNotFound()对象,该对象带有一条自定义消息,其中包含用于创建新页面的链接。不幸的是,链接被转义了。 如何强制html链接显示为链接

编辑: 我找到了一张解决方案表


很可能这样的对象已经存在于金字塔中

金字塔中的未找到视图接受与常规视图相同的谓词

config.add_route('wiki', '/wiki/{page}')

@notfound_view_config()
def notfound_view(exc, request):
    """ Generic notfound view for the entire site."""
    return exc

@notfound_view_config(route_name='wiki')
def wiki_notfound_view(exc, request):
    """ Specific notfound for urls matching the wiki pattern."""
    return exc

至于转义问题,这是特定于模板语言的。在mako中,您可以使用
${msg | n}
,在jinja2中,您可以使用
{{msg | safe}
关闭字符串的自动转义。

看起来您可以创建一个自定义的
未找到
视图,在该视图中可以呈现模板。@dm03514是的,但这会使事情变得更复杂。例如,我只希望在未找到wiki页面(即还不存在)时调用此自定义404视图,并且通常调用另一个默认404页面。
config.add_route('wiki', '/wiki/{page}')

@notfound_view_config()
def notfound_view(exc, request):
    """ Generic notfound view for the entire site."""
    return exc

@notfound_view_config(route_name='wiki')
def wiki_notfound_view(exc, request):
    """ Specific notfound for urls matching the wiki pattern."""
    return exc