塔架:如何编写自定义404页面? [英] Pylons: How to write a custom 404 page?
问题描述
So this question has been asked before, but not answered in great detail.
我想覆盖默认的Pylons错误页面,以创建更好的自定义页面.我已经覆盖了error.py
中的控制器,如下所示:
I want to override the default Pylons error page to make a nicer, custom one. I've got as far as overwriting the controller in error.py
, as follows:
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
这正常.我现在想做的是在模板目录中使用模板,以便404页面可以继承我通常的布局模板,CSS等.
This works OK. What I'd like to do now is use a template in the templates directory, so that the 404 page can inherit my usual layout template, CSS etc.
(我知道这对于500个错误是一个坏主意-在我使用模板而不是文字之前,我将在error.py
中检查代码是否为404.)
(I know this is a bad idea for 500 errors - I'll check in error.py
that the code is 404 before I use the template rather than a literal.)
所以,这是问题.如何定义custom_error_template
指向模板而不是文字?
So, here's the question. How do I define custom_error_template
to point at my template, rather than at a literal?
推荐答案
您应该能够使用render
方法(从yourapp.lib.base
导入它,然后使用return render('/path/to/error/template')
.
You should be able to use render
method (import it from yourapp.lib.base
, and use return render('/path/to/error/template')
.
这篇关于塔架:如何编写自定义404页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!