Django · Chapter 3 of 8
Django templates: inheritance, includes, filters, static
12 min study · Examples checked with Django 6.1 on Python 3.14 ·
Building HTML in Python strings, as the first chapter did, gets unreadable fast and makes it easy to forget escaping. Django’s template language keeps the HTML in separate files, fills in values from the view, and shares one page layout across the whole site. This chapter turns the catalog’s home page into a template-based list of lesson cards, adds a stylesheet and creates an “About” page with no view code at all.
Where templates live
With the default TEMPLATES setting (APP_DIRS: True), Django looks for templates in a templates folder inside each installed app. By convention each app nests another folder with its own name, so the files are referred to as catalog/home.html:
catalog/
templates/catalog/base.html
templates/catalog/home.html
templates/catalog/_lesson_card.html
templates/catalog/about.html
static/catalog/site.css
The repeated catalog looks redundant, but Django merges the template folders of all apps into one search path, and without it two apps with a home.html would collide.
A base template with blocks
Every page shares the same head, navigation and stylesheet, so they go into one base template:
{% load static %}<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>{% block title %}Study catalog{% endblock %}</title>
<link rel="stylesheet" href="{% static 'catalog/site.css' %}">
</head>
<body>
<nav><a href="{% url 'catalog:home' %}">Home</a> · <a href="{% url 'catalog:about' %}">About</a></nav>
<main>{% block content %}{% endblock %}</main>
</body>
</html>
{% block name %} marks a part that child templates may replace; the text inside is the default. {% url 'catalog:home' %} builds a link from a URL name, like reverse in Python, so links survive URL changes. {% load static %} enables {% static %}, which turns catalog/site.css into the right URL for wherever static files are served from.
A page that extends it
{% extends "catalog/base.html" %}
{% block title %}Lessons · Study catalog{% endblock %}
{% block content %}
<h1>{{ heading }}</h1>
<p>{{ lessons|length }} lesson{{ lessons|length|pluralize }}, {{ total_minutes }} minutes in total.</p>
{% for lesson in lessons %}
{% include "catalog/_lesson_card.html" %}
{% empty %}
<p>No lessons yet.</p>
{% endfor %}
{% endblock %}
{% extends %} must be the first tag; the child only fills in blocks. {% for %} loops over a list and {% empty %} covers the case of no items, which is easy to forget in plain loops. Each card comes from a separate file through {% include %}, which sees the same variables, including the loop’s lesson:
<article class="card">
<h2><a href="{% url 'catalog:lesson' lesson.id %}">{{ lesson.title|title }}</a></h2>
<p>{{ lesson.minutes }} min · {{ lesson.level|capfirst }}{% if lesson.minutes > 30 %} · long{% endif %}</p>
<p>{{ lesson.summary|default:"No summary yet."|truncatewords:12 }}</p>
</article>
The leading _ in the file name is a common hint that the template is a fragment, not a page.
Rendering from the view
In catalog/views.py, import render alongside redirect and replace home:
@require_GET
def home(request):
lessons = [{"id": pk, **data} for pk, data in LESSONS.items()]
context = {
"heading": "All lessons",
"lessons": lessons,
"total_minutes": sum(lesson["minutes"] for lesson in lessons),
}
return render(request, "catalog/home.html", context)
render loads the template, fills it with the context dictionary and returns an HttpResponse. Do the calculations, such as the total, in the view and pass the results; the template language deliberately has no arbitrary Python, which keeps templates simple and safe to edit. In templates, lesson.title works for dictionary keys, attributes and methods without arguments alike.
Filters and escaping
Filters transform a value with |: title capitalizes words, capfirst the first letter, length counts, pluralize adds an “s” when the number isn’t 1, default:"..." replaces empty values, truncatewords:12 shortens text, and date:"j M Y" formats dates. Change the third lesson in LESSONS to "title": "decorators <and> closures" with a "summary" key, reload, and the rendered card shows:
<h2><a href="/lessons/3/">Decorators <And> Closures</a></h2>
<p>40 min · Advanced · long</p>
The angle brackets became < and >. Django escapes every variable printed with {{ }} by default, so text from users or the database can’t inject HTML or scripts. That protection is on unless you switch it off with the |safe filter or {% autoescape off %}, which you should only do for HTML your own code produced. The {% if lesson.minutes > 30 %} tag shows that templates can compare, but heavier logic belongs in the view.
Static files
CSS, JavaScript and images go in the app’s static/catalog/ folder, catalog/static/catalog/site.css here (two lines of CSS for the card border and page width will do). In development, runserver serves them under STATIC_URL (/static/) automatically while DEBUG is on. In production Django doesn’t serve them: python manage.py collectstatic copies all apps’ static files into the folder named by STATIC_ROOT, and the web server, a CDN or a package such as WhiteNoise serves them from there. Templates don’t change, because {% static %} builds the right URL either way.
Pages without a view: TemplateView
A page with no data doesn’t need a view function. In catalog/urls.py, import TemplateView from django.views.generic and add:
path("about/", TemplateView.as_view(template_name="catalog/about.html"), name="about"),
about.html extends the base template and fills content with a heading and a paragraph. TemplateView is one of Django’s class-based generic views; chapter 8 uses others (ListView, CreateView) that handle common patterns such as listing and editing objects.
Practice
Create lesson_detail.html and change lesson_detail to render it instead of returning JSON, showing the title, minutes, level and summary, plus a “Back to all lessons” link built with {% url %}. Set the page title block to the lesson’s title.
Answer check: the view calls render(request, "catalog/lesson_detail.html", {"lesson": lesson, "lesson_id": lesson_id}) after the 404 check. The template starts with {% extends "catalog/base.html" %} and fills {% block title %}{{ lesson.title }} · Study catalog{% endblock %}. A missing summary shows the default text instead of an empty paragraph.
Summary
Keep templates in app/templates/app/, put the shared layout in a base template with blocks, and extend and include it. Views prepare data and call render; templates display it with tags and filters. Output is escaped automatically, static files live in app/static/app/ and are collected for production, and TemplateView serves simple pages.