Error handler not rendering template properly

So, I have error 404 handler in my project. When it is called, django doesn’t render the template properly.
Template

<script>
    authorize("{{ address }}")
</script>

Urls

handler404 = views.error_404

Views

def error_404(request, exception):
    return render(request, '404.html', context={"address": "http://192.168.1.150:8000/"})

So, I would expect it to be rendered like this:

<script>
    authorize("http://192.168.1.150:8000/")
</script>

But instead, it looks like this:

<script>
    authorize("")
</script>

When I render this template via normal view instead of error handler - it works properly. Does anyone know why this happens or how can I fix this?
Thanks!

Did you check error_404 is really called by breaking it in some way, e.g. assert 0 ? What you’re seeing implies {{ address }} can’t be found in the context so it looks like the view function isn’t being used!

Maybe it’s the line in your project/urls.py.

Instead of:
handler404 = views.error_404

It should be
handler404 = 'your_app.views.error_404'

More information can be found in the docs

I could be wrong but, wouldn’t assert 0 call the error 500 handler?

Turns out, error_404 actually wasn’t called, instead django was rendering my 404.html by itself. Nitinpaul’s advice helped fix it - before it was

handler404 = views.error_404

in my “my_app/urls.py”, after I changed it to

handler404 = 'my_app.views.error_404'

and put it in my “project/urls.py” it started working properly. Thanks a lot!

2 Likes

Hi, I am having similar issue. I am getting server 500 error instead of custom 404.html. See below.

I spent several hours on this code and could not get to find out why I am getting server error (500) instead of 404.html in django program. Please see below codes and could someone help?

settings.py: 
DEBUG = False
ALLOWED_HOSTS = ['*']

urls.py: (app is mysite)
handler404 = "mysite.views.error_404"

views.py: 
def error_404(request, exception):
    return render(request, '404.html')

404.html:
{% extends "base.html" %}
{% load static %}
{% block main %}

some html text here

{% endblock %}

Please advise?

This isn’t much to go on. What you’ve listed looks reasonable at first blush. Do you have a Django log to work from? Are you able to replicate this problem locally? If Django logging is set up in a standard way, then I think Django should be logging the error that triggered the 500 out to some kind of file.

I think you would have a traceback in the log that would tell you why Django is giving a 500 response instead of a 404 like you expect. If you can get that error, it would help immensely in debugging your problem.

Thank you for replying. I am not getting error or traceback in the Django log. See attached message from Django.image

I am running Django locally from my computer. This is why I cannot figure out where the issue is coming from because there is no error message on the wrong.

Can you share your logging configuration from your settings file? It seems like Django should be providing more information in this context.

Also, did you set the handler in the project’s urls.py or the application’s urls.py file?

The docs also indicate that the handler404 is supposed to return an HttpResponseNotFound. That may have nothing to do with it, but I’m trying to find things that might be off.

I just remembered that the docs say that the default 404 view will try to render a 404.html template if it can find one in the root of your templates directory.

It’s still interesting that you’re experiencing a failure, but you might be able to get around all of this by making sure your 404.html is in the right place and removing all of the handler404 configuration that you added.

I do not understand what you might by logging configuration.

Yes handler is in projects urls.py.

I even added 404.html in my apps templates and also templates in project as well. I tried removing handler404 configuration but still get server500 error.

How are you trying to trigger your 404 error?

-Jorge

You’re continuing to post a snippet of a log from 5 days ago.

Please change debug to True, try again, and post all the relevant information about this attempt. If it works with debug = True, then change to debug = False and try it again. If it works in one case and not the other, then you’re dealing with one of the items that are affected by a debug settings change. If it’s not different, then you should be seeing the debugging information either in the console or your logs.

Also, you’re making a reference to a URL ‘dsda’ - it would be helpful to see your urls.py file to see what view is associated with that url. It’s quite possible that this 500 error is being thrown from something outside the components you have posted.

If you are using 3.x, have you tried this shortcut?

from django.shortcuts import get_object_or_404, render

response = get_object_or_404(query, pk=query_id)

This will either return the page or display a 404 error.

I hope this helps