【问题标题】:How to include links in HTTPNotFound errors in Pyramid?如何在 Pyramid 的 HTTPNotFound 错误中包含链接?
【发布时间】:2013-04-04 13:07:28
【问题描述】:

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

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

编辑: 我找到了解决方案Python Pyramid & Chameleon templating language escapes html

class Literal:
    def __init__(self, s):
        self.s = s
    def __html__(self):
        return self.s

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

【问题讨论】:

标签: python http-status-code-404 pyramid html-escape-characters


【解决方案1】:

Pyramid 中的 Not Found 视图接受与常规视图相同的谓词。

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 }} 来关闭字符串的自动转义。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-26
    • 2015-11-23
    • 2020-10-16
    • 1970-01-01
    • 1970-01-01
    • 2011-02-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多