Django Template Language Cheatsheet
A comprehensive quick reference for Django's template syntax
Variables
Filters
Tags
Comments
Template Inheritance
Built-in Filters Reference
Built-in Tags Reference
CSRF & Security
Static Files
Custom Filters & Tags
{{ variable_name }}
{{ user.username }}
{{ article.title }}
{{ variable|default:"Nothing" }}
{{ variable|default_if_none:"None" }}
{{ variable|filter_name }}
{{ variable|filter_name:"argument" }}
{{ name|lower }}
{{ price|floatformat:2 }}
{{ text|escape|linebreaks }}
{{ name|truncatewords:10|lower }}
{{ html_content|safe }} # Disable auto-escaping
{% if %} - Conditional Rendering
{% if user .is_authenticated %}
Welcome, {{ user.username }}!
{% elif user .is_new %}
Please complete your profile
{% else %}
Please log in
{% endif %}
{% if age >= 18 %}
Adult
{% endif %}
{% if "admin" in user .groups %}
Admin access
{% endif %}
{% if user .is_authenticated and user .is_staff %}
Staff user
{% endif %}
{% if not user .is_banned %}
Active user
{% endif %}
{% for item in items %}
{{ item.name }}
{% empty %}
No items found
{% endfor %}
{% for user in users %}
{{ forloop.counter }} # Current iteration (1-indexed)
{{ forloop.counter0 }} # Current iteration (0-indexed)
{{ forloop.revcounter }} # Reverse counter
{{ forloop.revcounter0 }} # Reverse counter (0-indexed)
{{ forloop.first }} # First iteration?
{{ forloop.last }} # Last iteration?
{{ forloop.parentloop }} # Parent loop context
{% endfor %}
Comments
Single-line Comments (Not Rendered)
{# This is a comment #}
{# multi-line
comment #}
Multi-line Comment Template
{% comment %}
This is a multi-line comment
Anything here will be removed
{% endcomment %}
Base Template (base.html)
<!DOCTYPE html>
<html >
<head >
<title >{% block title %} My Site{% endblock %} </title >
{% block extra_css %}{% endblock %}
</head >
<body >
{% block header %}{% endblock %}
<main >
{% block content %}{% endblock %}
</main >
{% block footer %}{% endblock %}
{% block extra_js %}{% endblock %}
</body >
</html >
{% extends "base.html" %}
{% block title %} {{ page.title }} - My Site{% endblock %}
{% block content %}
<h1 >{{ page.heading }}</h1 >
{{ page.body|safe }}
{% endblock %}
{% block extra_css %}
{{ block.super }}
<link rel =" stylesheet" href =" /static/page.css" >
{% endblock %}
Block Super (Inherit Parent Content)
{% block content %}
{{ block.super }} # Include parent block content
<p >Additional content</p >
{% endblock %}
Built-in Filters Reference
Filter
Description
Example
lower
Converts to lowercase
{{ name|lower }}
upper
Converts to uppercase
{{ name|upper }}
title
Title case
{{ text|title }}
capitalize
First char uppercase
{{ text|capitalize }}
truncatechars:n
Truncate to n chars
{{ text|truncatechars:20 }}
truncatewords:n
Truncate to n words
{{ text|truncatewords:5 }}
wordcount
Count words
{{ text|wordcount }}
length
Return length
{{ list|length }}
length_is:n
Length equals n?
{{ list|length_is:3 }}
ljust:n
Left-align in n chars
{{ text|ljust:10 }}
rjust:n
Right-align in n chars
{{ text|rjust:10 }}
center:n
Center in n chars
{{ text|center:10 }}
cut:pattern
Remove pattern
{{ text|cut:" " }}
join:str
Join with string
{{ list|join:", " }}
Filter
Description
Example
add:n
Add n
{{ num|add:5 }}
sub:n
Subtract n
{{ num|sub:3 }}
floatformat:n
Round to n decimals
{{ price|floatformat:2 }}
filesizeformat
Human-readable size
{{ bytes|filesizeformat }}
Filter
Description
Example
first
First item
{{ list|first }}
last
Last item
{{ list|last }}
slice:n:m
Slice list
{{ list|slice:":2" }}
random
Random item
{{ list|random }}
dictsort:key
Sort by key
{{ dict|dictsort:"age" }}
dictsortreversed:key
Sort descending
{{ dict|dictsortreversed:"age" }}
unordered_list
Nested UL/LI
{{ list|unordered_list }}
Filter
Description
Example
date:format
Format date
{{ date|date:"D d M Y" }}
time:format
Format time
{{ time|time:"H:i" }}
timesince:val
Time since
{{ date|timesince:now }}
timeuntil:val
Time until
{{ date|timeuntil:now }}
Filter
Description
Example
escape
HTML escape
{{ html|escape }}
safe
Mark as safe
{{ html|safe }}
linebreaks
Convert newlines to <p>
{{ text|linebreaks }}
linebreaksbr
Convert newlines to <br>
{{ text|linebreaksbr }}
urlize
Convert URLs to links
{{ text|urlize }}
urlizetrunc:n
Truncate URL links
{{ text|urlizetrunc:15 }}
striptags
Strip HTML tags
{{ html|striptags }}
removetags
Remove specific tags
{{ html|removetags:"script style" }}
Filter
Description
Example
default:value
Default if falsy
{{ var|default:"N/A" }}
default_if_none:value
Default if None
{{ var|default_if_none:"N/A" }}
yesno:"a,b,c"
Yes/No/Maybe mapping
{{ flag|yesno:"Yes,No,Maybe" }}
divisibleby:n
Divisible by n?
{{ num|divisibleby:3 }}
Filter
Description
Example
slugify
Convert to slug
{{ title|slugify }}
escapejs
Escape for JavaScript
{{ text|escapejs }}
json_script
JSON in script tag
{{ data|json_script:"data-id" }}
localize
Localize output
{{ value|localize }}
localtime
Convert to local time
{{ date|localtime }}
urlencode
URL encode
{{ text|urlencode }}
Tag
Description
Example
block
Define block
{% block content %}
extends
Extend template
{% extends "base.html" %}
include
Include template
{% include "snippet.html" %}
load
Load template library
{% load static %}
Tag
Description
Example
if
Conditional
{% if condition %}
elif
Else if
{% elif condition %}
else
Else
{% else %}
endif
End if
{% endif %}
for
Loop
{% for item in items %}
empty
Empty loop case
{% empty %}
endfor
End for
{% endfor %}
ifequal
If equal (deprecated)
{% ifequal a b %}
ifnotequal
If not equal (deprecated)
{% ifnotequal a b %}
Tag
Description
Example
cycle
Cycle through values
{% cycle 'odd' 'even' %}
resetcycle
Reset cycle
{% resetcycle %}
firstof
First non-empty variable
{% firstof var1 var2 "fallback" %}
Tag
Description
Example
autoescape
Auto-escaping block
{% autoescape off %}
filter
Filter section
{% filter lower %}
spaceless
Remove whitespace
{% spaceless %}
verbatim
Disable template parsing
{% verbatim %}
Tag
Description
Example
for ... empty
Loop with empty case
{% for i in items %}...{% empty %}None{% endfor %}
cycle
Cycle through values
{% cycle 'row1' 'row2' %}
widthratio
Calculate ratio
{% widthratio this_value max_value max_width %}
with
Cache variable
{% with total=business.employees.count %}
Tag
Description
Example
csrf_token
CSRF token
{% csrf_token %}
url
URL routing
{% url 'view-name' param %}
static
Static file URL
{% static 'css/style.css' %}
Debugging & Comments
Tag
Description
Example
comment
Comment block
{% comment %}...{% endcomment %}
debug
Debug info
{% debug %}
Tag
Description
Example
trans
Translation
{% trans "Text" %}
blocktrans
Block translation
{% blocktrans %}...{% endblocktrans %}
language
Switch language
{% language 'en' %}
get_available_languages
Available langs
{% get_available_languages as langs %}
get_current_language
Current language
{% get_current_language as lang %}
Tag
Description
Example
timezone
Set timezone
{% timezone "UTC" %}
get_current_timezone
Get timezone
{% get_current_timezone as tz %}
localtime
Enable local time
{% localtime on %}
Tag
Description
Example
now
Current date/time
{% now "jS F Y H:i" %}
regroup
Regroup list
{% regroup people by gender as gender_list %}
lorem
Lorem ipsum text
{% lorem 3 p random %}
<form method =" post" >
{% csrf_token %}
{{ form.as_p }}
<button type =" submit" >Submit</button >
</form >
<script >
const csrftoken = document .querySelector (' [name=csrfmiddlewaretoken]' ).value ;
</script >
Safe Content (Disable Auto-escaping)
{{ html_content|safe }}
{% autoescape off %}
{{ html_content }}
{% endautoescape %}
<link rel =" stylesheet" href =" {% static 'css/style.css' %}" >
<script src =" {% static 'js/app.js' %}" ></script >
<img src =" {% static 'images/logo.png' %}" alt =" Logo" >
{% load static %}
{% static "css/style.css" as style_css %}
<link rel =" stylesheet" href =" {{ style_css }}" >
myapp/
├── templatetags/
│ ├── __init__.py
│ └── myapp_tags.py
# templatetags/myapp_tags.py
from django import template
register = template .Library ()
@register .filter
def multiply (value , arg ):
return value * arg
@register .filter (name = 'custom_upper' )
def custom_upper_filter (value ):
return value .upper ()
{% load myapp_tags %}
{{ price|multiply:2 }}
{{ name|custom_upper }}
@register .simple_tag
def current_time (format_string ):
return datetime .now ().strftime (format_string )
@register .simple_tag (takes_context = True )
def current_user (context ):
return context ['request' ].user
{% load myapp_tags %}
{% current_time "%Y-%m-%d %H:%M" %}
{% current_user %}
@register .inclusion_tag ('myapp/snippet.html' )
def show_results (results ):
return {'results' : results }
{% load myapp_tags %}
{% show_results poll .results %}
Template Context Processors
Built-in Context Processors
# settings.py
TEMPLATES = [{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
}]
{{ request.path }}
{{ request.user.username }}
{{ request.META.HTTP_USER_AGENT }}
{{ messages }}
Pagination
{% for object in page_obj %}
{{ object.name }}
{% endfor %}
<div class =" pagination" >
<span class =" page-links" >
{% if page_obj .has_previous %}
<a href =" ?page=1" >« ; first</a >
<a href =" ?page={{ page_obj.previous_page_number }}" >previous</a >
{% endif %}
<span class =" page-current" >
Page {{ page_obj.number }} of {{ page_obj.paginator.num_pages }}
</span >
{% if page_obj .has_next %}
<a href =" ?page={{ page_obj.next_page_number }}" >next</a >
<a href =" ?page={{ page_obj.paginator.num_pages }}" >last » ; </a >
{% endif %}
</span >
</div >
<form method =" post" >
{% csrf_token %}
{{ form.as_p }} {# Paragraph rendering #}
{{ form.as_table }} {# Table rendering #}
{{ form.as_ul }} {# List rendering #}
{# Manual rendering #}
{{ form.non_field_errors }}
{% for field in form %}
<div class =" field" >
{{ field.label_tag }}
{{ field }}
{{ field.errors }}
{% if field .help_text %}
<small >{{ field.help_text }}</small >
{% endif %}
</div >
{% endfor %}
<button type =" submit" >Submit</button >
</form >
{% if messages %}
<div class =" messages" >
{% for message in messages %}
<div class =" message {{ message.tags }}" >
{{ message }}
</div >
{% endfor %}
</div >
{% endif %}
Code
Description
Example Output
a
'a.m.' or 'p.m.'
'p.m.'
A
'AM' or 'PM'
'PM'
b
Month, textual, 3 letters
'Jan'
d
Day of month, 2 digits
'01' to '31'
D
Day of week, 3 letters
'Fri'
E
Month, locale specific
'listopada'
f
Time, 12-hour
'1', '1:30'
F
Month, textual, long
'January'
g
Hour, 12-hour
'1' to '12'
G
Hour, 24-hour
'0' to '23'
h
Hour, 12-hour, 2 digits
'01' to '12'
H
Hour, 24-hour, 2 digits
'00' to '23'
i
Minutes
'00' to '59'
I
Daylight Savings
'1', '0'
j
Day of month
'1' to '31'
l
Day of week, full
'Friday'
L
Leap year?
'True', 'False'
m
Month, 2 digits
'01' to '12'
M
Month, 3 letters
'Jan'
n
Month
'1' to '12'
N
Month abbreviation
'Jan'
O
Difference to GMT
'+0200'
P
Time
'1 a.m.', '1:30 p.m.'
r
RFC 2822
'Thu, 21 Dec 2000 16:01:07'
s
Seconds, 2 digits
'00' to '59'
S
English ordinal suffix
'st', 'nd', 'rd', 'th'
t
Days in month
'28' to '31'
T
Time zone
'EST', 'MDT'
u
Microseconds
'0' to '999999'
w
Day of week
'0' (Sunday) to '6' (Saturday)
W
ISO-8601 week
'1', '53'
y
Year, 2 digits
'99'
Y
Year, 4 digits
'1999'
z
Day of year
'0' to '365'
Z
Time zone offset
'-43200' to '43200'
Use {% load %} at the top of your template after {% extends %}
Always use {% csrf_token %} in POST forms
Use {% block super %} to extend parent block content
Cache expensive operations with {% with %} tag
Use {% static %} instead of hardcoded paths
Avoid complex logic in templates - keep it in views
Use {% spaceless %} to remove extra whitespace
Custom filters should return values, not print output
Template inheritance reduces code duplication significantly
Use {% include %} for reusable components
Generated for Django Template Language Quick Reference