I’m trying to find a way to customize error messages (404, 403) in my Pyramid application. I’ve found this doc, but it’s still not clear how to do it.
What I need to do it to render one of the templates (say, templates/404.pt) instead of standard 404 message. I’ve added following to my __init__.py:
from pyramid.config import Configurator
from pyramid.httpexceptions import HTTPNotFound
import myapp.views.errors as error_views
<...>
def main(global_config, **settings):
config = Configurator(settings=settings)
config.add_static_view('static', 'myapp:static')
config.add_route(...)
<...>
config.add_view(error_views.notfound, context=HTTPNotFound)
return config.make_wsgi_app()
Where error_views.notfound looks like
def notfound(request):
macros = get_template('../templates/macros.pt')
return {
'macros': macros,
'title': "HTTP error 404"
}
Sure it does not works (how do I specify template name in this case?), and even more: it seems view is not called at all and it’s code ignored.
You should pass to add_view as context a
pyramid.exceptionsexception, not apyramid.httpexceptionsone.This works for me: