Django template tags are a key feature of Django’s template system, enabling you to include dynamic content, control flow, and logic in templates. This reference guide, brought to you by The Coding College, provides an overview of essential Django template tags and their usage.
1. Basic Tags
{% block %}
and {% endblock %}
Defines a block of content that can be overridden in child templates.
Example:
{% block title %}Default Title{% endblock %}
{% comment %}
and {% endcomment %}
Adds comments to templates. Comments are not rendered in the output.
Example:
{% comment %} This is a comment and will not appear in the rendered page {% endcomment %}
2. Control Flow Tags
{% if %}
, {% elif %}
, and {% else %}
Used for conditional logic in templates.
Example:
{% if user.is_authenticated %}
<p>Welcome, {{ user.username }}!</p>
{% else %}
<p>Please log in.</p>
{% endif %}
{% for %}
and {% endfor %}
Loops through a list or iterable object.
Example:
<ul>
{% for item in items %}
<li>{{ item }}</li>
{% endfor %}
</ul>
{% empty %}
Used with the {% for %}
loop to display content when the iterable is empty.
Example:
<ul>
{% for item in items %}
<li>{{ item }}</li>
{% empty %}
<li>No items available.</li>
{% endfor %}
</ul>
3. Inheritance Tags
{% extends %}
Inherits from a parent template. Must be the first tag in a child template.
Example:
{% extends 'base.html' %}
{% include %}
Includes another template file.
Example:
{% include 'navbar.html' %}
4. Static and Media Tags
{% static %}
Generates a URL for static files.
Example:
<link rel="stylesheet" href="{% static 'css/styles.css' %}">
{% get_static_prefix %}
Returns the STATIC_URL prefix.
Example:
<p>Static prefix: {% get_static_prefix %}</p>
5. CSRF and URL Tags
{% csrf_token %}
Adds a CSRF token to forms for security.
Example:
<form method="post">
{% csrf_token %}
<input type="text" name="name">
</form>
{% url %}
Generates a URL for a named view.
Example:
<a href="{% url 'home' %}">Home</a>
6. Filters and Custom Tags
{% filter %}
Applies a filter to a block of content.
Example:
{% filter lower %}
THIS TEXT WILL BE LOWERCASE.
{% endfilter %}
{% with %}
Assigns a variable within a template for temporary use.
Example:
{% with total=order.total_price %}
<p>Total: {{ total }}</p>
{% endwith %}
7. Custom Template Tags
You can create your own template tags for specific functionality.
Example: Creating a Custom Tag
- Create a
templatetags
folder in your app. - Add an
__init__.py
file. - Create a
custom_tags.py
file:
Code in custom_tags.py
:
from django import template
register = template.Library()
@register.simple_tag
def multiply(value, multiplier):
return value * multiplier
Using the Tag in a Template:
{% load custom_tags %}
<p>Result: {% multiply 10 5 %}</p>
Final Thoughts
Django template tags are a powerful way to dynamically render content, manage control flow, and build reusable components in your web application. By mastering these tags, you can create clean, maintainable, and efficient templates.
For more Django tutorials, visit The Coding College!