djangodjango-templatesdjango-errors

Make django templates strict


In a django template, a call to {{ var }} will silently fail if var is undefined. That makes templates hard to debug. Is there a setting I can switch so django will throw an exception in this case?

The only hint at a solution I've found online is http://groups.google.com/group/google-appengine/browse_thread/thread/86a5b12ff868038d and that sounds awfully hacky.


Solution

  • Django<=1.9

    Set TEMPLATE_STRING_IF_INVALID = 'DEBUG WARNING: undefined template variable [%s] not found' in your settings.py.

    See docs:
    https://docs.djangoproject.com/en/1.9/ref/settings/#template-string-if-invalid

    Django>=1.10

    Set string_if_invalid = 'DEBUG WARNING: undefined template variable [%s] not found' template option in your settings.py.

    See docs: https://docs.djangoproject.com/en/2.0/topics/templates/#module-django.template.backends.django

    Also read: http://docs.djangoproject.com/en/dev/ref/templates/api/#invalid-template-variables