views:

123

answers:

4

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.

+4  A: 

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

Ned Batchelder
A: 

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.

Carl Meyer
+1  A: 

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
A: 

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

{% debug %}

http://docs.djangoproject.com/en/dev/ref/templates/builtins/#debug

Stefano
Very nice. Thank you.
blaine