PyCharm 2017.3 Help

Debugging Django Template Tutorial

This feature is supported in the Professional edition only.

Before you start

Make sure that:

  • You are working with PyCharm Professional Edition. This tutorial has been prepared with PyCharm version 2017.2.
  • Django is specified as the project template language. See section Configuring Template Languages for details.

Preparing an example

First, let's create a Django project MyDjangoProject1, with the application poll.

Next, open the file views.py for editing(F4) and see that the import statement is already there. In the second line you see the invitation to do something manually:

from django.shortcuts import render # Create your views here.

Type the following code:

from django.shortcuts import render def index(request): return render(request, 'index.html', context={'hello': 'world'})

You see that index.html is marked as unresolved reference:

/help/img/idea/2017.3/py_template_not_found.png

Press Alt+Enter or click /help/img/idea/2017.3/intentionBulb.png and choose to create the missing template:

/help/img/idea/2017.3/py_createTemplate.png

In the file index.html type the following code:

{% for char in hello %} {{ char }} {% endfor %}

It means that the value world of the variable hello will be printed one character after another.

Next, open for editing the urls.py file (F4), in this file find (Ctrl+F) the string url and after the url of the admin site, type the following code:

url(r'^index/', index),

Do not forget about the import statement!

from polls.views import index

You should end up with the following:

/help/img/idea/2017.3/py_url_index.png

So the example code is ready.

Adding breakpoint to a template

Let's add a breakpoint to the template index.html. To do that, open for editing the file index.html (F4) and click the left gutter:

/help/img/idea/2017.3/py_breakpointInDjangoTemplate.png

Editing and launching a run/debug configuration

Note that Django server run/debug configuration is created automatically, and the only thing required is editing it. To change this run/debug configuration, in the drop-down choose the Edit Configurations... command:

/help/img/idea/2017.3/py_edit_configurations.png

For example, change the port number. Here it will be 8123.

/help/img/idea/2017.3/py_django_server_run_configuration.png

Next, let's launch this run/debug configuration in the debug mode: choose this configuration from the drop-down and click /help/img/idea/2017.3/debug.png:

/help/img/idea/2017.3/py_dropdown_debug.png

You see that the name of the run/debug configuration in the drop-down, the /help/img/idea/2017.3/debug.png button and the Debug tool window are marked with a dot, which means that the run/debug configuration becomes active.

The run/debug configuration shows the error message (404), because neither admin site, nor index page are present. However, we need to suspend our application at the breakpoint. To do so, we need to add the name index to the contents of the address bar. The page is still not found, but in PyCharm you see the template with the hit breakpoint:

/help/img/idea/2017.3/py_breakpointInDjangoTemplateHit.png

and the Debug tool window.

Working with the Debug tool window

In the Django applications, all the functions of the Debug Tool Window are available, same as for the pure Python scripts. For example, you can step through your application, evaluate expressions, watch variables and more.

Stepping through the template

The stepping toolbar becomes active, and the stepping buttons are available. For example, click /help/img/idea/2017.3/frames_step_over.png and see that the value of the char variable changes to the next letter of the word world.

See the Stepping Through the Program section for details.

Evaluating an expression

Press Alt+F8, or, on the Stepping toolbar, click /help/img/idea/2017.3/variables_evaluate_expr.png. In the dialog box that opens, type the expression you wish to evaluate, and click Evaluate:

/help/img/idea/2017.3/py_evaluate_expression.png

See the Evaluating 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?

In the Variable tab, make sure that the /help/img/idea/2017.3/show-watches.png button is pressed (if this button is pressed, then the watches are visible in the Variables tab), and then click the /help/img/idea/2017.3/new-watch.png button. Type the name of the variable of interest, and now your watch is always on top of the Variables tab:

/help/img/idea/2017.3/py_watch_in_debug_tool_window.png

See the Adding, Editing and Removing Watches for details.

Summary

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 created the Django server run/debug configuration, and launched it in the debug mode.
  • Having hit the breakpoint, you've stepped through your template, evaluated expressions and added watches.

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

See Also

Procedures:

Language and Framework-Specific Guidelines:

Last modified: 15 September 2017