54

I'm new to Django and I wonder if there is a way to dump all the variables available to a template for debugging purposes. In Python I might use something like locals(), is there something equivalent for the default template engine?

Note: suppose I don't have access to the view for the purposes of this question.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Blaine Osepchuk
  • 1,067
  • 1
  • 9
  • 17

4 Answers4

80

Both Ned's and blaine's answers are good, but if you really want to achieve exactly what you ask for there's a template tag for it:

{% debug %}

Builtins:debug

More information in the context_processor.debug including:

If this processor is enabled, every RequestContext will contain debug and and sql_queries variables – but only if your DEBUG setting is set to True and the request’s IP address (request.META['REMOTE_ADDR']) is in the INTERNAL_IPS setting

Similar to Peter G suggestion, I often use a <div id="django-debug"><pre>{% debug|escape %}</pre></div> block at the end of the page that has display:none but that I can inspect to debug.

Stefano
  • 18,083
  • 13
  • 64
  • 79
  • 23
    To make this a bit more readable, one might use:
    {% filter force_escape %}{% debug %}{% endfilter %}
    – Peter G Apr 10 '12 at 15:51
  • @PeterG indeed, that's pretty much what I actually do myself too! And sometimes put that into a bottom-page element etc.etc. – Stefano Apr 10 '12 at 16:00
18

Install the Django Debug Toolbar. It gives you all that and more.

Ned Batchelder
  • 364,293
  • 75
  • 561
  • 662
10

You might also be interested in django-template-repl, a readline shell for the Django template language. You can drop a {% load repl %}{% pdb %} into your template and get an interactive debugger.

dbr
  • 165,801
  • 69
  • 278
  • 343
Carl Meyer
  • 122,012
  • 20
  • 106
  • 116
6

While the two solutions provided by the other members may get you access to all the variables in a template I thought there had to be an easier way (thanks for your responses, BTW).

Here is a simple way to find all the variables passed to the template.

  1. Introduce an error into the template you want to examine. Adding a non-existent tag works.
  2. Ensure that debugging is ON.
  3. Browse to the page that loads the template. (your site must be running via runserver or some other means).

The debugging output for the template contains a section called "TraceBack". Find the traceback for your view (second entry from the top in my case) and click on "Local vars". And it's all there.

Blaine Osepchuk
  • 1,067
  • 1
  • 9
  • 17
  • While this method works well for one-off debugging, I have found that I ask the question "what variables are available in this template?" enough that it's worth the effort to install the django-debug-toolbar mentioned by @ned-batchelder. Incidentally, it doesn't take much effort to get it installed (install the package, update settings.py appropriately), and the default configuration is good for most scenarios. – hlongmore Apr 03 '18 at 20:46