I have a custom 404 view defined in my Pyramid app:
@view_config(context=HTTPNotFound, renderer='404.pt')
def not_found(self, request):
return {}
It works fine, except that the HTTP status code sent with the content is 200 OK, which is not OK by any means. I'm having the same problem with 403 Forbidden. How can I get Pyramid to send the correct status code?
The exception view is a separate view that provides a spot for you to do whatever you want. Just like any view that uses a renderer, you can affect the response object via request.response
to modify its behavior. The renderer then fills in the body.
@view_config(context=HTTPNotFound, renderer='404.pt')
def not_found(self, request):
request.response.status = 404
return {}
Actually, in pyramid 1.3 There's a new decorator @notfound_view_config.
@notfound_view_config(renderer = '404_error.jinja2')
def notfound(request):
request.response.status = 404
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With