Django debug display all variables of a page

DjangoVariablesTemplatesDebugging

Django Problem Overview


Is there a template tag (or any other trick) I can use to display all the variables available in a page?

Django Solutions


Solution 1 - Django

If DEBUG is enabled, there's a template tag called {% debug %}

Solution 2 - Django

There are several options (some of them already listed before):

  1. django builtin debug tag - I use this boilerplate to display it properly:

> <pre> {% filter force_escape %} {% debug %} {% endfilter %} > </pre>

  1. 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)

  2. 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 - Django

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 - Django

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 - Django

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.

Example of django-debug-toolbar template debugging

Solution 6 - Django

If you use pycharm of professional version, you can set breakpoints on some lines in a template file and view the variable value.

For more detail, follow this link.https://www.jetbrains.com/help/pycharm/debugging-django-templates.html

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestioneamanView Question on Stackoverflow
Solution 1 - DjangoKeryn KnightView Answer on Stackoverflow
Solution 2 - DjangoRobert LujoView Answer on Stackoverflow
Solution 3 - DjangoCapuchinView Answer on Stackoverflow
Solution 4 - DjangoTomView Answer on Stackoverflow
Solution 5 - DjangoAaron C. de BruynView Answer on Stackoverflow
Solution 6 - DjangoW.PerrinView Answer on Stackoverflow