TEMPLATE_STRING_IF_INVALID doesn't work for me. A quick fix is to open env/lib64/python2.7/site-packages/django/template/base.py, find except Exception and throw a print e inside it (assuming you're using manage.py runserver and can see print output).
However, a few lines down is current = context.template.engine.string_if_invalid. I noticed string_if_invalid was empty despite having set TEMPLATE_STRING_IF_INVALID. This lead me to this part of the docs:
https://docs.djangoproject.com/en/1.8/ref/templates/upgrading/#the-templates-settings
Django’s template system was overhauled in Django 1.8 when it gained support for multiple template engines.
...
If your settings module defines ALLOWED_INCLUDE_ROOTS or TEMPLATE_STRING_IF_INVALID, include their values under the 'allowed_include_roots' and 'string_if_invalid' keys in the 'OPTIONS' dictionary.
So in addition to @slacy's TemplateSyntaxError trick,
class InvalidString(str):
def __mod__(self, other):
from django.template.base import TemplateSyntaxError
raise TemplateSyntaxError(
"Undefined variable or unknown value for: %s" % other)
TEMPLATE_STRING_IF_INVALID = InvalidString("%s")
you also need to define string_if_invalid as follows
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
'OPTIONS': {
'string_if_invalid': TEMPLATE_STRING_IF_INVALID,
...
Straight away this found a bunch of issues I had I didn't even know about. It really should be enabled by default. To solve tags and filters that expect to fail silently I threw conditionals around them:
{% if obj.might_not_exist %}
{{ obj.might_not_exist }}
{% endif %}
Although I suspect this only works because the {% if %} fails silently. Another approach might be to create a hasattr filter: {% if obj|hasattr:"might_not_exist" %}.