Django's templating language is a lightweight, secure, and Python-like syntax used to build dynamic HTML pages. It lets you insert variables, loop through data, use conditionals, and call filters directly in your templates using the context passed from the view. It avoids direct Python code in templates for security and separation of concerns, making your views responsible for logic and templates focused on presentation.
Templating Language
What is it?
Syntax
Variables
| Action | Syntax | Description |
|---|---|---|
| Display a variable |
{{ variable_name }}
|
Renders a variable passed from the view context |
| Nested attribute |
{{ user.first_name }}
|
Access attributes of objects |
| List index (0-based) |
{{ my_list.0 }}
|
Access list items by index |
| Dictionary key |
{{ my_dict.key_name }}
|
Access dictionary value using dot notation |
For Loops
| Action | Syntax | Description |
|---|---|---|
| Loop over a list |
{% for item in items %} ... {% endfor %}
|
Iterates over items |
| Show fallback if list empty |
{% for item in items %}{% empty %}No items.{% endfor %}
|
Displays message if list is empty |
| Loop metadata (counter, etc.) |
{{ forloop.counter }}
|
Starts at 1, use forloop.counter0 for 0-based |
If Statements
| Condition Type | Syntax | Notes |
|---|---|---|
| Basic if |
{% if condition %} ... {% endif %}
|
Checks truthiness of variable or condition |
| If / else |
{% if condition %} ... {% else %} ... {% endif %}
|
Two-branch logic |
| If / elif / else |
{% if a %} ... {% elif b %} ... {% else %} ... {% endif %}
|
Multi-branch condition |
| Compare values |
{% if value == 10 %}
|
You can use ==, !=, >, <, >=, <= |
| Check existence |
{% if my_list %}
|
True if list has any items |
| Logical AND / OR |
{% if a and b %} / {% if a or b %}
|
Basic logic supported |
| Negation |
{% if not user.is_authenticated %}
|
"not" also supported |
Filters
Filters follow the variable wrapped in the double curly brackets by a pipe (|). For example in the capitalise row of the table below the full syntax would be {{ variable | capfirst }} and the rest follow this {{ variable | filter name }} syntax.
| Filter | Filter Name |
|---|---|
| Capitalise | capfirst |
| Lowercase | lower |
| Uppercase | upper |
| Default fallback | default:"Unknown" |
| Length of list/string | length |
| Date formatting | date:"F j, Y" |
| Safe HTML | safe |
| Truncate chars/words | truncatechars:20 or truncatewords:10 |
Tags and blocks:
| Action | Syntax | Usage |
|---|---|---|
| Load static files |
{% load static %}
|
Used at the top of templates for static files |
| Static file reference |
<img src="{% static 'img/logo.png' %}">
|
Load images, CSS, JS |
| Template inheritance |
{% extends "base.html" %}
|
Inherit from a base template |
| Define block in base |
{% block content %}{% endblock %}
|
Mark sections to be filled by child templates |
| Include a template |
{% include "includes/navbar.html" %}
|
Insert reusable chunks like navbars |
| Comment out code |
{# This is a comment #}
|
Won't be rendered in output |