Failed lookups in technical_404 template


When my Django application raises an Http404 exception in debug mode, I get multiple errors like "Exception while resolving variable 'name' in template 'unknown'." in my log.

I chased it down to Django’s technical_404.html template, which asks for the “name” attribute of URL patterns, which does not exist in some of them:

{% if forloop.last and %}[name='{{ }}']{% endif %}

Should I ignore it, or does it mean that I have the URL patterns set up incorrectly?

Welcome @gollux !

You should ignore it.

One of Django’s strengths (in my opinion) is that it is valid to reference a variable name in a template when that variable doesn’t necessarily exist in the context. It makes your templates able to be a lot more flexible and dynamic.

This also isn’t an issue in a production deployment, because you would never run your production environment with DEBUG = True.

Thanks for your reply!

I also consider it Django’s strength, but the error messages clutter my debug log, making problems in my own code harder to spot.

Is it possible to turn off the “undefined variable” errors while keeping the rest of debug messages?

Tangentially, is it possible to check if a variable is defined inside a template?

It is. The logger being used is django.template, so you can modify your logger configuration to handle this however desired.

The if tag is about as close as I’m aware of, but it doesn’t make a distinction between existance and a falsy value.