📝 Django

Django Templates: Introduction

P
Author
Pyland
📅
Published
30.06.2026
⏱️
Reading time
1 min
👁️
Views
88
🌱
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

📝

pytest-django: Testing Django

Охватываемые темы: Installation, @pytest.mark.djangodb, Fixtures, Testing views.

📅 30.06.2026 👁️ 138
📝

What is an ORM

ORM (Object-Relational Mapping) is a technology that lets you work with a database through Python...

📅 30.06.2026 👁️ 131
📝

AI Agents: ReAct Loop and Autonomous Actions

A chatbot answers questions. An agent takes action: it calls tools, retrieves real data, and...

📅 30.06.2026 👁️ 106

Did you like the article?

Subscribe to our updates and receive new articles first. Grow with PyLand!