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 <https://docs.djangoproject.com/en/1.7/ref/settings/#std:setting-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 != "" %} On Wednesday, 8 February 2017 12:08:44 UTC+2, Florian Perrodin wrote: > > 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 > > > -- 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+unsubscr...@googlegroups.com. To post to this group, send email to django-users@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/f9d123d5-0758-4923-9ce8-c4afe80b3c61%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.