[Django]-Django – Custom handler500 not working despite following docs

3👍

FOUND THE SOLUTION

I had a setting called DEBUG_PROPAGATE_EXCEPTIONS set to True. This seemed to disable my custom handler. It works perfectly now.

3👍

Thanks Juan, it is necessary set DEBUG = False. Doc reference

If DEBUG is set to True (in your settings module), then your 500 view will never be used, and the traceback will be displayed instead, with some debug information

-2👍

Quoting Django documentations

If you implement a custom view, be sure it accepts a request argument and returns an HttpResponseServerError.
You have to return an HttpResponseServerError in your handler500.

A very simple way to implement all this is just include in your views

def handler500(request, exception, template_name="my_template.html"):
    response = render_to_response("my_template.html")
    response.status_code = 500
    return response

This way you don’t need to change anything in your URLConfs. This approach does require that your template is at the top of the root template folder. Maybe name it “500.hml” and it would jump up to the top.

Leave a comment