开发者

Pylons: How to write a custom 404 page?

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('S开发者_开发技巧CRIPT_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?


You should be able to use render method (import it from yourapp.lib.base, and use return render('/path/to/error/template').


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜