Add Django templates
IntelliJ IDEA 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.
Create a template for a view
Suppose you reference a template file that doesn't yet exist. IntelliJ IDEA 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 IntelliJ IDEA automatically discovers the directories specified in the
TEMPLATE_DIRS
orTEMPLATE_LOADERS
fields 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 page of the Settings dialog, plus the directories, which are automatically detected in the
TEMPLATE_DIRS
orTEMPLATE_LOADERS
variables 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
page of the Settings dialog and ensure that file types of any existing templates are properly recognized.Configure a template language for a project
Open the Settings/Preferences dialog, and click the node Java Template Languages.
In the
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.
You can make Django support skip some folders (disable Django language substitution), if you mark them as resources.
When creating a Django application, you can immediately specify a folder where templates will be stored.
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.
Mark a folder as a template directory
In the Project tool window, right-click the desired directory.
From the context menu, choose
, and then click the checked command .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.
IntelliJ IDEA 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.
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
.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.
Then click the button on the main toolbar, or press Shift+F9.
IntelliJ IDEA 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:
Examine the rendering contexts in the Variables pane.
Step through the breakpoints defined within the Django template.
Use the debugging console .