[Answered ]-How to use handler404 view in django?

2👍

You don’t need to do any of this. It is, of course, Django’s default behaviour to return the 404 page if it does not match any of the URLs in the urlconf.

The default handler is almost always suitable, you just need to define a template called 404.html in the root of your templates directory. The only reason to define a custom handler is if you need extra context variables, or if you want to do custom logic (eg logging) on a 404 error.

0👍

The reason for this is that you did not include an exception argument to the handler.

Instead of

def handler404(request):
  from django.shortcuts import render
  return render(request, '404.html')

It needs to be

def handler404(request, exception):
  from django.shortcuts import render
  return render(request, '404.html')

Please refer to the documentation here: https://docs.djangoproject.com/en/2.2/ref/urls/#django.conf.urls.handler404

👤Max

Leave a comment