PyCharm
 
Get PyCharm
You are viewing the documentation for an earlier version of PyCharm.

Debug Django templates

Last modified: 21 June 2023

Before you start, ensure that Django is specified as the project template language.

The Django server run/debug configuration is created automatically. If required, you can edit it by selecting the Edit Configurations command in the run/debug configuration list on the main toolbar:

Edit configurations

For example, you can choose to open a browser window automatically when the configuration is launched:

Run/Debug configuration for a Django server

The Debug tool window for Django applications has all the functions, similar to pure Python scripts:

  • Stepping through the program

    The stepping toolbar is active, and the stepping buttons are available. For example, you can click Step over and see that the value of the char variable changes to the next letter of the word world.

    See the Step through the program section for details.

  • Evaluating expressions

    Press AltF8, or click Evaluate expression on the Stepping toolbar. In the dialog that opens, type the expression you wish to evaluate, and click Evaluate:

    Evaluate expressions

    See the Evaluate expressions section for details.

  • Watching variables

    Suppose, you'd like to always keep an eye on a certain variable of a template, say, char. How to do that?

    At the top of the Debugger tab of the Debug tool window, type the name of the variable of interest, and press Add to watch:

    Add a watch for a variable

    The value of the variable will be displayed at the top of the variables list through the rest of the debugging process. See the Watches section for details.

That's it... What has been done here? Let's repeat:

  • You've created a Django project, with a template in it.

  • You've added a breakpoint to this template.

  • You've launched the Django server run/debug configuration in the debug mode.

  • Having hit the breakpoint, you've learned how to step through your template, evaluate expressions, and add watches.