Django Template Language (DTL)

The Django Template Language is the syntax used inside Django templates: {{ }} for variables, {% %} for tags (logic), and | for filters that transform values. It is intentionally restricted to keep logic out of templates.

Django

DTL has three core constructs. Variables {{ user.name }} output data and can dig into attributes, dictionary keys, and list indices with dot notation. Tags {% if %}, {% for %}, {% block %} add control flow and structure. Filters {{ name|upper }} transform a value, and they chain: {{ text|truncatewords:30|capfirst }}.

By design DTL is not a full programming language. You cannot call arbitrary Python or pass arbitrary arguments to functions; the philosophy is that complex logic belongs in views, not templates. When you outgrow the built-ins, you write a custom template tag or filter instead.

DTL auto-escapes HTML in variables to prevent XSS, so {{ user_input }} is safe by default; use the |safe filter only when you are certain the content is trusted.

Syntax

{{ value|filter:arg }}   {% tag %}

Examples

Python

<!-- variables, tags, and filters together -->
{% for p in posts %}
  <h2>{{ p.title|title }}</h2>
  <p>{{ p.body|truncatewords:20 }}</p>
  <small>{{ p.created|date:"M d, Y" }}</small>
{% endfor %}

Learn it in a lesson

See also

Back to the Dictionary