Lesson 20 of 60 – Django Template Inheritance
33%

Django Template Inheritance

Django Template Inheritance allows multiple HTML pages to share a common layout. Instead of writing the same header, navigation, footer, and other common HTML on every page, we can create a base template and allow other templates to extend it.

Note: Template inheritance mainly uses the {% extends %} and {% block %} template tags.

1. What is Template Inheritance?

Template inheritance is a Django feature that allows one template to reuse the structure of another template.

A common layout can be stored in a file such as base.html. Other pages can then extend that template.

2. Why Use Template Inheritance?

Template inheritance helps us:

  • Avoid repeating HTML code
  • Create a common website layout
  • Maintain headers and footers easily
  • Reuse navigation menus
  • Keep templates organized
  • Make large websites easier to maintain

3. The Base Template

The base template contains the common structure of the website.

base.html

It may contain the HTML document structure, navigation, header, footer, and content blocks.

4. Basic base.html Structure

<!DOCTYPE html>
<html>

<head>
    <title>My Website</title>
</head>

<body>

    <header>
        My Website
    </header>

    {% block content %}
    {% endblock %}

    <footer>
        Copyright
    </footer>

</body>

</html>

5. The block Tag

The block tag defines an area that a child template can replace or fill.

{% block content %}
{% endblock %}

The name content is chosen by the developer and can be changed.

6. The extends Tag

The extends tag tells Django that a template should inherit from another template.

{% extends 'base.html' %}

This is normally placed near the beginning of a child template.

7. Creating a Child Template

Suppose we create a file called home.html.

{% extends 'base.html' %}

{% block content %}

<h1>Home Page</h1>

<p>Welcome to my website.</p>

{% endblock %}

8. How extends Works

The basic process is:

base.html
     ↓
home.html extends base.html
     ↓
Django combines the templates
     ↓
Final HTML page

The child template provides content for the blocks defined by the base template.

9. Header in Base Template

<header>

    <h1>My Website</h1>

    <nav>
        Home | About | Contact
    </nav>

</header>

Because this is inside the base template, child pages can reuse the header.

10. Footer in Base Template

<footer>

    <p>Copyright 2026 My Website</p>

</footer>

The footer can be written once and shared by many pages.

11. Creating Multiple Blocks

A base template can contain multiple blocks.

<title>

{% block title %}
My Website
{% endblock %}

</title>

{% block content %}
{% endblock %}

12. Overriding the Title Block

A child template can replace the title block.

{% extends 'base.html' %}

{% block title %}
Home Page
{% endblock %}

{% block content %}

<h1>Welcome Home</h1>

{% endblock %}

13. Multiple Child Templates

A single base template can be extended by many pages.

base.html
    ↓
home.html
about.html
contact.html
services.html
blog.html

All these pages can share the same website structure.

14. Template Folder Structure

myapp/
│
├── views.py
├── models.py
│
└── templates/
    ├── base.html
    ├── home.html
    ├── about.html
    └── contact.html

15. Basic Base Template Example

<!DOCTYPE html>
<html>

<head>
    <title>{% block title %}My Website{% endblock %}</title>
</head>

<body>

    <header>
        <h1>My Website</h1>
    </header>

    {% block content %}
    {% endblock %}

    <footer>
        <p>Footer</p>
    </footer>

</body>

</html>

16. Basic Child Template Example

{% extends 'base.html' %}

{% block title %}
Home
{% endblock %}

{% block content %}

<h2>Home Page</h2>

<p>Welcome to our website.</p>

{% endblock %}

17. The block.super Variable

The block.super variable can be used when a child template wants to keep the original content of a block and add additional content.

{% block content %}

{{ block.super }}

<p>Additional content</p>

{% endblock %}

18. Adding to a Base Block

For example, if the base template has:

{% block content %}
<p>Common content</p>
{% endblock %}

A child template can preserve it using:

{% block content %}

{{ block.super }}

<p>Child content</p>

{% endblock %}

19. Including Reusable Components

Template inheritance can be combined with the include tag.

{% include 'navbar.html' %}

{% block content %}
{% endblock %}

{% include 'footer.html' %}

This allows common components to be maintained separately.

20. Template Inheritance with Static Files

Static CSS and JavaScript files can be included in the base template so that child pages automatically use the common resources.

{% load static %}

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

21. Navigation in a Base Template

<nav>

    <a href="{% url 'home' %}">Home</a>

    <a href="{% url 'about' %}">About</a>

    <a href="{% url 'contact' %}">Contact</a>

</nav>

The navigation can then be shared by all child templates.

22. Template Inheritance and Views

Views continue to render the child templates normally.

from django.shortcuts import render

def home(request):
    return render(request, 'home.html')

The child template extends the base template, and Django builds the final response.

23. Passing Variables to an Inherited Template

View:

def home(request):

    data = {
        'name': 'Rahul'
    }

    return render(
        request,
        'home.html',
        data
    )

Child template:

{% extends 'base.html' %}

{% block content %}

<h1>Welcome {{ name }}</h1>

{% endblock %}

24. Nested Template Inheritance

Django also allows a template to extend another child template.

base.html
    ↓
section.html
    ↓
article.html

This can be useful for larger websites with several levels of shared layouts.

25. Common Base Layout

A professional website may use a base template containing:

  • DOCTYPE and HTML structure
  • Meta information
  • CSS files
  • Navigation bar
  • Header
  • Main content block
  • Footer
  • JavaScript files

26. Common Template Inheritance Mistakes

  • Incorrect base template name
  • Incorrect template path
  • Forgetting {% extends %}
  • Using a block name that does not exist in the parent
  • Forgetting {% endblock %}
  • Incorrect indentation or HTML structure
  • Incorrect static file configuration

27. Important Rule for extends

The extends tag should normally appear as the first template tag in a child template.

{% extends 'base.html' %}

{% block content %}
    <h1>Home Page</h1>
{% endblock %}

The child template then overrides the appropriate blocks.

28. Complete Website Structure

templates/
│
├── base.html
├── home.html
├── about.html
├── contact.html
├── services.html
│
└── partials/
    ├── navbar.html
    └── footer.html

This structure can help organize reusable templates in a Django project.

29. Complete Template Inheritance Example

base.html

{% load static %}

<!DOCTYPE html>
<html>

<head>

    <title>
        {% block title %}
        My Website
        {% endblock %}
    </title>

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

</head>

<body>

    <header>
        <h1>My Website</h1>
    </header>

    <nav>
        <a href="{% url 'home' %}">Home</a>
        <a href="{% url 'about' %}">About</a>
    </nav>

    <main>

        {% block content %}
        {% endblock %}

    </main>

    <footer>
        <p>Copyright 2026</p>
    </footer>

</body>

</html>

home.html

{% extends 'base.html' %}

{% block title %}
Home Page
{% endblock %}

{% block content %}

<h2>Welcome to Home Page</h2>

<p>
This content is specific to the home page.
</p>

{% endblock %}

30. Summary of Template Inheritance

Django Template Inheritance is an important feature for creating reusable and maintainable website layouts.

  • base.html commonly contains the shared layout.
  • {% extends %} allows a child template to inherit a parent template.
  • {% block %} defines replaceable sections.
  • Multiple child templates can extend the same base template.
  • block.super can preserve parent block content.
  • {% include %} can reuse separate template components.
  • Static files can be loaded in the base template.
  • Named URLs can be used in shared navigation.
  • Template inheritance reduces duplicate HTML code.

📌 Key Points

  • Template inheritance allows templates to reuse a common layout.
  • The extends tag connects a child template to a parent template.
  • The block tag defines areas that child templates can customize.
  • A base template can contain headers, navigation, footers, CSS, and JavaScript.
  • Multiple pages can extend the same base template.
  • block.super can preserve content from the parent block.
  • Template inheritance helps reduce duplicate code.
  • It makes large Django websites easier to maintain.

🧠 Quick Quiz

Question: Which Django template tag is used to inherit a base template?