Add Django 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.
To create a template for a view
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".
Place the caret at the unresolved reference to a template.
Press Alt+Enter, or click the yellow light bulb to show the list of available intention actions.
From the suggestion list, choose action Create template <name>:
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_LOADERSfields of the settings.py file. You can specify the other directories in addition.
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_LOADERSvariables of the settings.py file.
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
Open the Settings/Preferences dialog, and click the node Python Template Languages.
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
In the Settings/Preferences dialog Ctrl+Alt+S, click the Project Structure page.
Choose the directory to be marked as a template root.
Do one of the following:
Click on the toolbar of the Content roots pane.
Choose Templates on the directory's context menu.
To mark a folder as a template directory
You can make Django support skip some folders (disable Django language substitution), if you mark them as resources, for example:
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
Place breakpoints at the desired lines of the Django templates:
Launch Django server in the Debug mode.
This is how it's done:
From the main menu, choose.
Then click the button 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.
In the Debug tool window, you can: