Home > OS >  I can't get Django custom error pages to work
I can't get Django custom error pages to work

Time:01-06

I am trying to get custom error pages (404 & 500) to work in my Django application.

So far I have the following:

urls.py

handler404 = views.error_404
handle500 = views.error_500

views.py

def error_500(request, exception):
    return render(request, '500.html', status=500)

def error_404(request, exception):
    return render(request, '404.html', status=404)

DEBUG is set to FALSE.

However when I try access an error page I get the following error:

"A server error occurred. Please contact the administrator."

Which (obviously) is not my error template.

Any help appreciated!

CodePudding user response:

handler404 = pages.views.error_404
handle500 = pages.views.error_500

ADD (pages.views.error_404 and error_500)

CodePudding user response:

You have a typo in your urls.py file. Use:

handler500 = views.error_500

Instead of:

handle500 = views.error_500

Here is the Django docs link for more detail. PS: You can also pass the path of your custom view as a string like this.

handler500 = "your_app.views.error_500"

This is just a matter of preference.

  • Related