[Django]-Django handler404 not called on raise Http404()

10👍

You don’t have to write your own handlers if you just want to replace Django’s template. Django uses 404.html and 500.html files for templates and you can replace them with putting file with same name to the directory listed in settings.TEMPLATE_DIRS.

In your example that means that you can create myapp/templates/404.html and myapp/templates/500.html files.

Note that you have to set DEBUG to False to se those templates.

Check this article for mor details.

1👍

I ran into a similar situation. My custom handler404 WAS being called, yet my browser response showed the error500.

Upon inspection, I found I had a syntax error in the template. Thus, the 500 was being caused by that. I fixed the template and the result was as desired.

The catch 22 is that by default, template debug == debug, so if you set DEBUG=False then the template debug will be false as well; you’ll see the 500 error without the template error trace.

So, place a print() statement in your handler404 view, right before returning render() . If you see the output in your django console, you’ll know the error is in your template.

You could also try this in your settings.py (django 1.8+) but this didn’t seem to work for me, and I didn’t follow through once I fixed the template.

TEMPLATES = [
    {

        'OPTIONS': {
             'debug': True,
         }
    },

1👍

The 404-page-not-found-view needs an "exception" argument:

def handler404(request, exception):
    return render(request, 'customer/home_page/404.html', status=404)
👤vivek

0👍

Another solution is to replace (monkey-patch) Django’s page_not_found.

from django.views import defaults
from myapp.views import not_found_view

defaults.page_not_found = not_found_view

Monkey-patching is bad practice and may stop working if Django updates internal API, but still may be useful in some situations. Ex. if other methods don’t work on some reason.

👤ribozz

Leave a comment