PyCharm
 
Get PyCharm

Part 2. Debugging Django Templates

Last modified: 28 June 2024

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.

    For more information, refer to Step through the program.

  • Evaluating expressions

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

    Evaluate expressions

    For more information, refer to Evaluate expressions.

  • 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 variable list through the rest of the debugging process. For more information, refer to Watches.

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.

The next step is also intended for the Professional edition users - this is Debugging JavaScript.