you might want to pause in the middle of a template to inspect the state (say, inside a loop). A debugger would be perfect for such cases. In fact, it is possible to use any one of the aforementioned Python debuggers for your templates using custom template tags.
Here is a simple implementation of such a template tag. Create the following file inside a templatetag package directory:
# templatetags/debug.py
import pudb as dbg # Change to any *db
from django.template import Library, Node
register = Library()
class PdbNode(Node):
def render(self, context):
dbg.set_trace() # Debugger will stop here
return ''
@register.tag
def pdb(parser, token):
return PdbNode()
In your template, load the template tag library, insert the pdb tag wherever you need the execution to pause, and enter the debugger:
{% load debug %}
{% for item in items %}
{# Some place you want to break #}
{% pdb %}
{% endfor %}
Within the debugger, you can examine anything, including the context variables using the context dictionary:
>>> print(context["item"])
Item0
If you need more such template tags for debugging and introspection, then I would recommend that you check out the django-template-debug package