The Django Template Language, DTL, interpolates Python into HTML.
<html><head>{% block titl e %}{% endlbock %}</head><body>{% block content %}{% endblock %}</body></html>
Templates are what users see on a web page.
<h1>Page title</h1>
Templates are stored in .
Template tags are used to inject Python content.
{{% block content %}}{{% endblock %}}
Content tags can be used to extend templates to reduce repeated code.
<!DOCTYPE html><html><head>{% block headBlock %}{% endblock %}</head><body><h1>Hello World!</h1>{% block content %}{% endblock %}</body></html>
A base template can be used as the basis for other templates using the extends
tag.
{% extends "base.html" %}
Static files, such as CSS, images, and JavaScript files, are placed in a separate location in the project— /static/ by default.
myProject/└── myProject/└── myapp/└── static/
Templates can load static files using the `{% load static %} tag in the header of the HTML file. Specific files can be loaded by specifying the exact file path as an argument.
{% load static "custom.css" %}
Variables can be displayed using variable tags.
{{ variable_name }}
Templates can include logic using if
statements.
{% if conditional_variable %}{% endif %}
Template variables can be iterated over using for
-in
loops inside template tags.
<ul>{% for item in dictionary %}<li>{{ item.value }}</li>{% endfor %}</ul>
URLs can be linked using the url
tag.
{% url "some-other-page" %}
URL tags can include arguments to pass between webpages.
{% url ""blog-view"" blog.id %}
Filters can be applied with the |
symbol.
{{ variable_name | filter_name }}
Arguments can be passed to filters using the :
symbol after the filter name.
{{ variable_name | filter_name:"argument"}}
Dictionaries can be sorted using the dictsort
filter.
{{ dictionary_name | dictsort:"key_name"}}
Some filters require variables of a certain data type.
{{ date_time | time"H": "i" }}