Django debug display all variables of a page
Solution 1
If DEBUG is enabled, there's a template tag called {% debug %}
Solution 2
There are several options (some of them already listed before):
-
django builtin debug tag - I use this boilerplate to display it properly:
<pre> {% filter force_escape %} {% debug %} {% endfilter %} </pre>
Use django template debug application's tags like attributes, variables and details or you can even set a breakpoint inside of a template where you can inspect everything with pdb debugger (or ipdb)
Django debug toolbar - has a template panel for this purpose
Usually all debug features work only when the app is in DEBUG mode.
Solution 3
Pro tip. Use textarea
and auto select onclick
for easier copy-paste:
<textarea onclick="this.focus();this.select()" style="width: 100%;"> {% filter force_escape %} {% debug %} {% endfilter %}</textarea>
Solution 4
The debug toolbar does all this and much, much more. See the screencast for more. If you literally just want the variables, you could try
assert False, locals()
in your view
Solution 5
A slightly more complex solution with better rewards is to load django-debug-toolbar (documentation here)
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.
eaman
Updated on July 08, 2022Comments
-
eaman almost 2 years
Is there a template tag (or any other trick) I can use to display all the variables available in a page?
-
eaman over 14 yearsThnaks. As I'm using a generic view, is there a way to summon a "assert False, locals()"
-
Derek Organ over 13 yearsguys, I've debug toolbar installed and working but it doesn't display all the variables available to me in the template... am i missing something.. where does this assert false, locals() go and what exactly does it do?
-
Tom over 13 yearsIf you've written a custom view, just put it near the end of your view. If you're using a generic view, you'd need to wrap it in your own view. I think that would work, anyway.
-
tread over 6 yearsI am getting
'<<triggers database query>>'
as the value -
Aaron C. de Bruyn over 6 yearsI would read the documentation and if that fails, I would go to the django-debug-toolbar project and request help: github.com/jazzband/django-debug-toolbar