所以这个问题之前已被问过 https://stackoverflow.com/questions/2706602/pylons-changing-the-regular-404-not-found-page-to-a-custom-one,但没有详细回答。
我想覆盖默认的 Pylons 错误页面以制作一个更好的自定义页面。我已经覆盖了控制器error.py
, 如下:
def document(self):
"""Render the error document"""
resp = request.environ.get('pylons.original_response')
content = literal(resp.body) or cgi.escape(request.GET.get('message', ''))
custom_error_template = literal("""\
# some brief HTML here
""")
page = custom_error_template % \
dict(prefix=request.environ.get('SCRIPT_NAME', ''),
code=cgi.escape(request.GET.get('code', str(resp.status_int))),
message=content)
return page
这工作正常。我现在想做的是使用templates目录中的模板,这样404页面就可以继承我常用的布局模板、CSS等。
(我知道对于 500 个错误来说这是一个坏主意 - 我会检查一下error.py
在我使用模板而不是文字之前,代码是 404。)
那么,问题来了。我如何定义custom_error_template
指向我的模板,而不是文字?
你应该能够使用render
方法(从导入它yourapp.lib.base
,并使用return render('/path/to/error/template')
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)