1

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

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

编辑:我找到了Python Pyramid & Chameleon 模板语言转义 html的解决方案

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

Pyramid 中很可能已经存在这样的对象

4

1 回答 1

2

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

于 2013-04-04T15:14:28.243 回答