Django Tags

Template logic and control structures

🏷️ What are Django Tags?

Django template tags provide programming logic in templates. Tags are enclosed in curly braces with percent signs and enable loops, conditions, and other dynamic template operations.


<!-- Django tag syntax -->
{% if user.is_authenticated %}
    <p>Welcome back!</p>
{% endif %}
                                    

Common Django Tags

🔀

if/else

Conditional logic in templates

{% if condition %}
  ...
{% endif %}
🔁

for

Loop through lists and objects

{% for item in list %}
  ...
{% endfor %}
🔗

url

Generate URLs from view names

{% url 'home' %}
📄

include

Include other templates

{% include 'header.html' %}

🔹 URL Tag

The URL tag generates URLs dynamically based on view names defined in your urls.py file. This prevents hardcoding URLs and makes your templates more maintainable and flexible.

🔸 urls.py

from django.urls import path
from . import views

urlpatterns = [
    path('', views.home, name='home'),
    path('about/', views.about, name='about'),
    path('contact/', views.contact, name='contact'),
]

🔸 Template

<nav>
    <a href="{% url 'home' %}">Home</a>
    <a href="{% url 'about' %}">About</a>
    <a href="{% url 'contact' %}">Contact</a>
</nav>

🔹 Block and Extends Tags

Block and extends tags enable template inheritance. Create a base template with blocks that child templates can override, promoting code reuse and consistent layouts across pages.

🔸 Base Template (base.html)

<!DOCTYPE html>
<html>
<head>
    <title>{% block title %}My Site{% endblock %}</title>
</head>
<body>
    <header>
        <h1>My Website</h1>
    </header>
    
    {% block content %}
    {% endblock %}
    
    <footer>
        <p>© 2025 My Site</p>
    </footer>
</body>
</html>

🔸 Child Template (home.html)

{% extends 'base.html' %}

{% block title %}Home - My Site{% endblock %}

{% block content %}
    <h2>Welcome to Home Page</h2>
    <p>This is the home page content.</p>
{% endblock %}

Output:

My Website

Welcome to Home Page

This is the home page content.

© 2025 My Site

🔹 Static Tag

The static tag loads static files like CSS, JavaScript, and images. It automatically generates the correct path to your static files directory for proper resource loading.

🔸 Template

{% load static %}

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="{% static 'css/style.css' %}">
</head>
<body>
    <img src="{% static 'images/logo.png' %}" alt="Logo">
    <script src="{% static 'js/script.js' %}"></script>
</body>
</html>

Note: You must use {% load static %} at the top of your template before using the static tag.

🔹 CSRF Token Tag

The CSRF token tag protects forms from Cross-Site Request Forgery attacks. Django requires this token in all POST forms to verify request authenticity and enhance security.

<form method="post">
    {% csrf_token %}
    <input type="text" name="username">
    <input type="password" name="password">
    <button type="submit">Login</button>
</form>

Output:

🔹 Now Tag

The now tag displays the current date and time with custom formatting. It's useful for showing timestamps, copyright years, or any time-sensitive information in templates.

<p>Current date: {% now "Y-m-d" %}</p>
<p>Current time: {% now "H:i:s" %}</p>
<p>Full datetime: {% now "F j, Y, g:i a" %}</p>
<p>Copyright © {% now "Y" %}</p>

Output:

Current date: 2025-01-15

Current time: 14:30:45

Full datetime: January 15, 2025, 2:30 p.m.

Copyright © 2025

💡 Important Tips:

  • Tags are enclosed in {% %} curly braces with percent signs
  • Most tags require a closing tag: {% if %}...{% endif %}
  • Use {% load %} to load custom tag libraries
  • Tags provide logic without Python code in templates
  • Always include {% csrf_token %} in POST forms

🧠 Test Your Knowledge

What is the correct syntax for Django template tags?