Django HTML Templating: Mastering Templates in Django

Ann Jul 09, 2026

Django, a high-level Python web framework, offers a powerful templating engine that allows developers to create dynamic HTML pages. This engine, based on the concept of template inheritance and extension, enables separation of concerns, promoting clean and maintainable code. Let's delve into the intricacies of Django's HTML templating system.

Django Templates for CRUD Views
Django Templates for CRUD Views

At its core, Django's templating system is designed to render dynamic content onto HTML pages. It uses a simple and expressive syntax, making it easy for developers to create reusable and maintainable templates. The engine supports control structures like if-elif-else and for loops, allowing for conditional rendering and iteration over data.

Django Template Language (DTL) Basics
Django Template Language (DTL) Basics

Template Inheritance and Extension

One of the key features of Django's templating system is its support for template inheritance and extension. This allows developers to create a base template (or parent template) with common elements like headers, footers, and navigation menus. Child templates can then extend this base template, inheriting its structure and adding or overriding specific sections.

Chapter 12: Implementing Schema Markup
Chapter 12: Implementing Schema Markup

Inheritance promotes code reuse and consistency across your website. It also makes it easy to update common elements in one place, with changes propagating to all child templates. Django's template inheritance is defined using the `{% extends %}` template tag and block tags like `{% block %}` and `{% endblock %}`.

Base Templates

a green and white brochure with information about the different types of items on it
a green and white brochure with information about the different types of items on it

A base template, also known as a parent template, serves as the foundation for your website's layout. It typically contains the site's header, footer, and navigation menu. Here's a simple example of a base template named `base.html`:

<!DOCTYPE html>
<html>
  <head>
    <title>My Website</title>
  </head>
  <body>
    <header>Header</header>
    <nav>Navigation</nav>
    <main>{% block content %}{% endblock %}</main>
    <footer>Footer</footer>
  </body>
</html>

In this example, the `{% block content %}` tag defines a section where child templates can add their content.

Child Templates

Django Project Structure Best Practice 2022
Django Project Structure Best Practice 2022

Child templates extend the base template, inheriting its structure and adding or overriding specific sections. Here's an example of a child template named `home.html` that extends `base.html`:

{% extends 'base.html' %}

{% block content %}
  <h1>Welcome to my website!</h1>
  <p>This is the home page content.</p>
{% endblock %}

In this child template, the `{% block content %}` tag is used to add content to the `content` block defined in the base template.

Template Tags and Filters

Template for List Page in Django
Template for List Page in Django

Django's templating system comes with a wide range of built-in template tags and filters that allow developers to manipulate variables, control the flow of the template, and format output. These tags and filters enable developers to create dynamic and interactive web pages.

Some of the most commonly used template tags include `{% if %}`, `{% for %}`, `{% with %}`, and `{% url %}`. Filters like `{{ variable|escape }}`, `{{ variable|truncatewords:10 }}`, and `{{ variable|date:"F j, Y" }}` allow developers to format and manipulate variables before they are rendered onto the page.

🗑️ Template for Delete Page in Django
🗑️ Template for Delete Page in Django
Django Project Structure Explained (Beginner Guide)
Django Project Structure Explained (Beginner Guide)
📊 Django Templates vs Django APIs: Key Differences
📊 Django Templates vs Django APIs: Key Differences
Django: MVT(Model-View-Template)
Django: MVT(Model-View-Template)
Basic CRUD Structure in Django
Basic CRUD Structure in Django
Managing Static Files in Django Development
Managing Static Files in Django Development
Customize Django Allauth Templates for a Seamless User Experience
Customize Django Allauth Templates for a Seamless User Experience
Customize Views: Add Extra Context in Django
Customize Views: Add Extra Context in Django
Add Links with the {% url %} Tag in Django Templates
Add Links with the {% url %} Tag in Django Templates
Understanding Context in Django Templates
Understanding Context in Django Templates
the diagram shows how to use django
the diagram shows how to use django
Hando - Django Admin & Dashboard Template
Hando - Django Admin & Dashboard Template
14 Django Website Template | Online Training Download app from below link
14 Django Website Template | Online Training Download app from below link
a diagram with the words django in different languages
a diagram with the words django in different languages
Learn Django Project  and File Structure
Learn Django Project and File Structure
| Pythonista Planet
| Pythonista Planet
Customize Your Django Allauth Auth Pages
Customize Your Django Allauth Auth Pages
Add URL Patterns in Django – Direct Requests to Views
Add URL Patterns in Django – Direct Requests to Views
Understanding Django's MVT Framework
Understanding Django's MVT Framework
Django Basic | Day 02
Django Basic | Day 02

Template Tags

Template tags are used to control the flow of the template and manipulate variables. Here are a few examples:

  • `{% if user.is_authenticated %}`: Renders content only if the user is authenticated.
  • `{% for item in items %}`: Loops through a list of items and renders content for each item.
  • `{% with total=items|length %}`: Sets a variable `total` to the length of the `items` list and makes it available within the block.
  • `{% url 'view_name' arg1 arg2 %}`: Generates a URL for a specific view with the given arguments.

Template Filters

Template filters are used to format and manipulate variables before they are rendered onto the page. Here are a few examples:

  • `{{ variable|escape }}`: Escapes any HTML special characters in the variable to prevent XSS attacks.
  • `{{ variable|truncatewords:10 }}`: Truncates the variable to the first 10 words.
  • `{{ variable|date:"F j, Y" }}`: Formats the variable as a date string in the format "Month day, year".
  • `{{ variable|pluralize }}`: Pluralizes the variable if it's not equal to 1.

Django's templating system is powerful, flexible, and easy to use. It allows developers to create dynamic, reusable, and maintainable HTML pages. Whether you're building a simple website or a complex web application, Django's templating system has the tools you need to create engaging and interactive user experiences.

Now that you've learned about Django's HTML templating system, it's time to put this knowledge into practice. Start by creating a base template for your website, then create child templates that extend this base template. Experiment with template tags and filters to create dynamic and interactive content. With Django's templating system, the possibilities are endless.