Django showing error 500 instead of showing error 404

Asked

Viewed 80 times

0

My file views.py:

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

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

My file urls.py:

handler404 = general_views.handler404
handler500 = general_views.handler500

urlpatterns = [
    (...)
]

When I try to access in the browser some URL that does not exist in my urlpatterns of the archive urls.py, always render the 500 error screen instead of rendering the 404 error screen.

1 answer

0

According to the django documentation, in the part dealing with custom error pages, it is mandatory that the custom view of error 404 accepts two parameters: the request and the exception.

The default is the parameter exception in the view handler404 was causing an error and so was always shown error 500 (internal error on the server). Also according to the documentation, the view handler500 has only one parameter.

The correct code for the file view.py is:

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.