๐Ÿ“ Django

Django Templates: Introduction

P
Author
PyLand Team
๐Ÿ“…
Published
30.06.2026
โฑ๏ธ
Reading time
1 min
๐Ÿ‘๏ธ
Views
286
๐ŸŒฑ
Level
Beginner

Django Template Language (DTL) is a simple templating language for generating HTML.

Basic Syntax

<!-- Variables: {{ variable }} -->
<h1>{{ task.title }}</h1>
<p>{{ user.get_full_name }}</p>

<!-- Tags: {% tag %} -->
{% if user.is_authenticated %}
  <p>Hello, {{ user.username }}!</p>
{% else %}
  <p>Please log in to continue</p>
{% endif %}

<!-- Filters: {{ variable|filter }} -->
<p>{{ task.created_at|date:"d.m.Y" }}</p>
<p>{{ text|truncatewords:20 }}</p>

The for Loop

<ul>
{% for task in tasks %}
  <li>{{ forloop.counter }}. {{ task.title }}</li>
{% empty %}
  <li>No tasks</li>
{% endfor %}
</ul>

Loop variables:
- forloop.counter โ€” iteration number (starting from 1)
- forloop.counter0 โ€” iteration number (starting from 0)
- forloop.first, forloop.last โ€” first/last iteration

Conditionals

{% if task.status == 'done' %}
  <span>โœ“ Completed</span>
{% elif task.status == 'in_progress' %}
  <span>โณ In Progress</span>
{% else %}
  <span>โ—‹ Queued</span>
{% endif %}

Inheritance (extends/block)

<!-- base.html -->
{% block title %}My Site{% endblock %}
{% block content %}{% endblock %}

<!-- page.html -->
{% extends 'base.html' %}
{% block title %}Tasks โ€” My Site{% endblock %}
{% block content %}
  <h1>Tasks</h1>
{% endblock %}

URL Tags

<a href="{% url 'task-list' %}">All Tasks</a>
<a href="{% url 'task-detail' pk=task.pk %}">{{ task.title }}</a>

Loading Tags

{% load static %}
<link rel="stylesheet" href="{% static 'css/style.css' %}">

{% load i18n %}
{% trans "Tasks" %}

Your reaction to the article

๐Ÿ’ฌ Comments (0)

๐Ÿ” Sign in to leave a comment
๐Ÿšช Login
๐Ÿ’ญ

No comments yet

Be the first to share your opinion about this article!

๐Ÿ”— Similar

Similar articles

Continue learning with these materials

๐Ÿ“

Django REST Framework: Introduction

DRF is the most popular package for building REST APIs in Django.

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 290
๐Ÿ“

Search in Django

You can start with simple Django ORM filters and move to PostgreSQL features when you...

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 279
๐Ÿ“

Swagger Documentation with drf-spectacular

drf-spectacular generates an OpenAPI 3.0 schema and Swagger UI for DRF.

๐Ÿ“… 30.06.2026 ๐Ÿ‘๏ธ 563
๐ŸŽ“ Continue learning

Courses that cover this material

Visit the course to apply this material in practice.

Django RequestLab: from HTTP to secure production Open course curriculum Markup for Backend Developers Open course curriculum