Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get all the variables defined in a Django template?

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.

like image 489
Blaine Osepchuk Avatar asked Jan 14 '10 19:01

Blaine Osepchuk


People also ask

How can you display all variables in the page on to a Django template?

There's an option called 'Templates' with another option to 'Toggle context' and you can see all the variables passed to your template, as well as the ability to see the code behind the template.

How do you define a variable in a template?

In the template, you use the hash symbol, # , to declare a template variable.

What does Django template contains?

Being a web framework, Django needs a convenient way to generate HTML dynamically. The most common approach relies on templates. A template contains the static parts of the desired HTML output as well as some special syntax describing how dynamic content will be inserted.


2 Answers

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

like image 43
Ned Batchelder Avatar answered Oct 10 '22 21:10

Ned Batchelder


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.

like image 120
Stefano Avatar answered Oct 10 '22 22:10

Stefano