Templates are responsible for what users see in a Django application. While views handle logic, templates handle presentation (HTML). They allow you to create dynamic web pages by combining static HTML with data from your backend.
In this tutorial, you will learn how Django templates work, how to use variables, loops, conditions, and how to build reusable layouts.
A template is an HTML file that contains special Django syntax to display dynamic data.
<h1>Hello {{ name }}</h1>{{ name }} is a variableThe flow is:
Inside your app (blog), create this structure:
blog/
└── templates/
└── blog/
└── home.htmlhome.html<!DOCTYPE html>
<html>
<head>
<title>My Blog</title>
</head>
<body>
<h1>Welcome to my blog</h1>
</body>
</html>In views.py:
from django.shortcuts import render
def home(request):
return render(request, 'blog/home.html')Now visiting /blog/ will display your HTML page.
You can pass data from views to templates.
def home(request):
return render(request, 'blog/home.html', {
'name': 'Youssef'
})<h1>Hello {{ name }}</h1>Hello Youssefdef home(request):
return render(request, 'blog/home.html', {
'name': 'Youssef',
'age': 25
})
<p>Name: {{ name }}</p>
<p>Age: {{ age }}</p>Filters modify variables inside templates.
{{ variable|filter }}{{ name|upper }}
{{ name|lower }}
{{ name|length }}Youssef → YOUSSEF7Templates support loops using {% for %}.
def home(request):
posts = ['Post 1', 'Post 2', 'Post 3']
return render(request, 'blog/home.html', {'posts': posts})<ul>
{% for post in posts %}
<li>{{ post }}</li>
{% endfor %}
</ul>Templates support conditions with {% if %}.
{% if user %}
<p>Welcome {{ user }}</p>
{% else %}
<p>Please login</p>
{% endif %}Tags are used for logic in templates.
{% if %}{% for %}{% block %}{% extends %}{% include %}Instead of repeating HTML, you can create a base layout.
base.html<!DOCTYPE html>
<html>
<head>
<title>{% block title %}My Site{% endblock %}</title>
</head>
<body>
<header>
<h1>My Website</h1>
</header>
<main>
{% block content %}{% endblock %}
</main>
</body>
</html>home.html{% extends 'blog/base.html' %}
{% block title %}Home{% endblock %}
{% block content %}
<h1>Welcome to my blog</h1>
{% endblock %}
You can reuse parts of templates.
{% include 'blog/navbar.html' %}Useful for:
To use CSS/JS/images, load static files.
{% load static %}<link rel="stylesheet" href="{% static 'css/style.css' %}">{# This is a comment #}Django automatically escapes HTML for security.
{{ user_input }}Prevents XSS attacks.
render(request, 'home.html') ❌
render(request, 'blog/home.html') ✅{% load static %}{% endfor %} or {% endif %}{{ if name }} ❌
{% if name %} ✅def home(request):
posts = [
{'title': 'Post 1'},
{'title': 'Post 2'}
]
return render(request, 'blog/home.html', {'posts': posts}){% extends 'blog/base.html' %}
{% block content %}
<h1>Blog</h1>
<ul>
{% for post in posts %}
<li>{{ post.title }}</li>
{% endfor %}
</ul>
{% endblock %}Think of templates like a TV screen.
In this tutorial, you learned how Django templates work and how to create dynamic HTML pages. You used variables, loops, conditions, filters, and template inheritance to build reusable and structured pages.
Templates are essential for creating user interfaces in Django.
render() connects views to templates{{ }} is used for variables{% %} is used for logic{{ }} mean?{% for %} used for?{% load static %} do?Templates bring your Django application to life by displaying dynamic content to users. Once you understand templates, you can build beautiful and interactive web pages.