我的网站的一部分是维基引擎。当页面不存在时,我想提供一个自定义的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


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

最佳答案

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

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

关于python - 如何在 Pyramid 中的HTTPNotFound错误中包含链接?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/15812127/

10-11 22:04
查看更多