Search code examples
djangodjango-templatesdjango-template-filters

Check if variable exists in template, without causing errors in loggers if it doesn't


I have enabled 'level': 'DEBUG' in LOGGING in settings.py.

I'm aware that the proposed solutions to check if a variable exists, in a template, is using the if template tag

{% if variable %}

This is proposed in the documentation and questions asking how to check if a variable exists are closed as off-topic and pointed in that direction.

Another solution offered here is to compare with None.

{$ if variable is not None %}

however, in both cases, although it works fine on the user end, the logger saves that as a KeyError, cluttering my log files.

How can I avoid this?


Solution

  • You can set the log level for the logger named django.template to INFO or higher to disable these log messages.