问题
So this question has been asked before, but not answered in great detail.
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
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.
(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.)
So, here's the question. How do I define custom_error_template
to point at my template, rather than at a literal?
回答1:
You should be able to use render
method (import it from yourapp.lib.base
, and use return render('/path/to/error/template')
.
回答2:
Just create your view and use add_notfound_view
configuration method to configure it.
See: http://docs.pylonsproject.org/docs/pyramid/en/latest/api/config.html?highlight=document%20error#pyramid.config.Configurator.add_notfound_view
来源:https://stackoverflow.com/questions/4449336/pylons-how-to-write-a-custom-404-page