Debugging Django templates
Projects can have very complicated logic in their templates. Subtle bugs while creating a template can lead to hard-to-find bugs. We need to set TEMPLATE_DEBUG
to True
(in addition to DEBUG
) in settings.py
so that Django shows a better error page when there is an error in your templates.
There are several crude ways to debug templates, such as inserting the variable of interest, such as {{ variable }}
, or if you want to dump all the variables, use the built-in debug
tag like this (inside a conveniently clickable text area):
<textarea onclick="this.focus();this.select()" style="width: 100%;">
{% filter force_escape %}
{% debug %}
{% endfilter %}
</textarea>
A better option is use the Django Debug Toolbar mentioned earlier. It not only tells you the values of the context variables but also shows the inheritance tree of your templates.
However, you might want to pause in the middle of a template to inspect the state (say, inside...