Logs bloated by template variable not found in DEBUG level

141 views
Skip to first unread message

Florian Perrodin

unread,
Feb 8, 2017, 5:08:44 AM2/8/17
to Django users
Dear all,

What is the proper way of checking if a variable exists in django?
To my knowledge, the proper way is to use the {% if var %} tag. If this is the case, it should not trigger a print in the output (even in the debug log level). Hereafter is an example:

# Note: django 1.10.2
from django.template import Context, Template

context = Context({})
template = Template("{% if avar %}bla{% endif %}")

# output
##################################
DEBUG 2017-02-08 10:19:14,844 base 3090 139658596226880 Exception while resolving variable 'avar' in template 'unknown'.
Traceback (most recent call last):
  File "***/python3.4/site-packages/django/template/base.py", line 885, in _resolve_lookup
    current = current[bit]
  File "***site-packages/django/template/context.py", line 75, in __getitem__
    raise KeyError(key)
KeyError: 'avar'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "***site-packages/django/template/base.py", line 891, in _resolve_lookup
    if isinstance(current, BaseContext) and getattr(type(current), bit):
AttributeError: type object 'Context' has no attribute 'avar'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "***site-packages/django/template/base.py", line 900, in _resolve_lookup
    current = current[int(bit)]
ValueError: invalid literal for int() with base 10: 'avar'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "***site-packages/django/template/base.py", line 907, in _resolve_lookup
    (bit, current))  # missing attribute
django.template.base.VariableDoesNotExist: Failed lookup for key [avar] in "[{'True': True, 'False': False, 'None': None}, {}]"
##################################

I however agree that the following code should generate an debug trace

from django.template import Context, Template

context = Context({})
template = Template("{{ avar }}")


Is there a workaround? Another way to check properly if a variable exists? - Note that I want to see other DEBUG messages, so I don't want to change the log level

Best regards

Florian


Matthew Pava

unread,
Feb 8, 2017, 8:58:53 AM2/8/17
to django...@googlegroups.com

I would define avar in the context inside the view function with a value of None (or other falsey value).

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/c8cfc93e-3426-48a9-ab77-5154f496f9a2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Derek

unread,
Feb 8, 2017, 9:23:01 AM2/8/17
to Django users
If the variable does not exist at all (which is strange, because you should be passing in all the variables needed by the template, from the view that calls it), then the docs say:

If you use a variable that doesn’t exist, the template system will insert the value of the TEMPLATE_STRING_IF_INVALID setting, which is set to '' (the empty string) by default.
https://docs.djangoproject.com/en/1.7/topics/templates/#variables

so your test could look like:

{% if avar != "" %}
Reply all
Reply to author
Forward
0 new messages