PyCharm 2019.3 Help

Templates

PyCharm makes it possible to create and render templates written in one of the supported template languages:

It is important to note that one can edit templates without actually installing the template languages. However, in order to create or render templates, and navigate between views and templates, the corresponding template language should be properly installed.

Creating a template from usage

Suppose you reference a template file that doesn't yet exist. PyCharm marks such a reference as unresolved, and provides an intention action to create a template file "from usage".

  1. Place the caret at the unresolved reference to a template.

  2. Press Alt+Enter, or click the yellow light bulb to show the list of available intention actions.

  3. From the suggestion list, choose action Create template <name>:

    create a template

    Create Template dialog appears, showing the read-only template name (Template path field), and a list of possible template locations (Templates root field).

    Note that PyCharm automatically discovers the directories specified in the TEMPLATE_DIRS or TEMPLATE_LOADERS fields of the settings.py file. You can specify the other directories in addition.

  4. In the Create Template dialog, select the template directory, where the new template will be created.

    The Template root field provides a list of possible locations for the new template. This list includes the template directories specified in the Template Languages page of the Settings dialog, plus the directories, which are automatically detected in the TEMPLATE_DIRS or TEMPLATE_LOADERS variables of the settings.py file.

  5. Click OK. The empty .html file with the name is created in the specified location.

Before making use of a certain template language, configure it in the Template Languages page of the Settings dialog.

To configure a template language for a project

  1. Open the Settings/Preferences dialog, and click the node Python Template Languages.

  2. In the Template Languages page, do the following:

    • From the Template language list, select the specific template language to be used in project.

    • In the Template file types area, specify the types of files, where template tags will be recognized.

      Note that in HTML, XHTML, and XML files templates are always recognized.

      Use Add and Remove buttons to make up the desired list of file types.

When creating a Django application, you can immediately specify a folder where templates will be stored.

To define template directories

  1. In the Settings/Preferences dialog Ctrl+Alt+S, click the Project Structure page.

  2. Choose the directory to be marked as a template root.

  3. Do one of the following:

    • Click Templates on the toolbar of the Content roots pane.

    • Choose Templates on the directory's context menu.

To mark a folder as a template directory

  1. In the Project Tool Window, right-click the desired directory.

  2. From the context menu, choose Mark Directory As, and then click the checked command Template Directory.

    This results in adding the marked directory to the list of template directories in the Project Structure page.

You can make Django support skip some folders (disable Django language substitution), if you mark them as resources, for example:

Mark a folder as a resource root

PyCharm allows placing breakpoints to the lines of Django template files, at the lines with Django tags or expressions. If you want to trace back exceptions that are raised in course of template debugging, open Breakpoints dialog,  and in the Django Exception Breakpoints tab, select the checkbox Suspend. Debugging Jinja and Mako templates is not supported.

To debug a Django template

  1. Place breakpoints at the desired lines of the Django templates:

    breakpoints

  2. Launch Django server in the Debug mode.

    This is how it's done:

    1. From the main menu, choose Run | Edit Configurations.

    2. If the desired Django Server run/debug configuration exists, select it, otherwise create a new one, as described in the section Creating and Editing Run/Debug Configuration.

    3. Then click the button start debugger on the main toolbar, or press Shift+F9.

    PyCharm will open the template in your browser, and suspend at the breakpoints you've set.

    The Debugger session starts, and Debug tool window appears.

  3. In the Debug tool window, you can:

Last modified: 2 April 2020