djangodjango-errors

How to handle 404 request in Django?


I am trying to handle 404 requests when the page is not found. I am getting an error as Server Error (500).

Here is the code :

In settings.py

DEBUG = False
ALLOWED_HOSTS = ['localhost', '127.0.0.1']

In myapp.views:

def handle_not_found(request, exception):
    return render(request, "404.html")

In project urls.py :

handler404 = "myapp.views.handle_not_found"

Error on Browser :

Page not found (404)
Request Method: GET
Request URL:    http://127.0.0.1:8000/requested_page

Using the URLconf defined in myproject.urls, Django tried these URL patterns, in this order:

URLs shows here...

The current path, requested_page, didn’t match any of these.

Solution

  • I am getting this error because the template which I render through 404 handers has an error.

    I have fixed that error and now it working fine.