Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pyramid: Custom 404 page returns as "200 OK"

Tags:

python

pyramid

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?

like image 248
Theron Luhn Avatar asked Mar 22 '12 01:03

Theron Luhn


2 Answers

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 {}
like image 86
Michael Merickel Avatar answered Nov 14 '22 03:11

Michael Merickel


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
like image 45
ajorquera Avatar answered Nov 14 '22 01:11

ajorquera