WikiGalaxy

Personalize

Template Inheritance in Django

Understanding Template Inheritance

Template inheritance in Django allows you to create a base template that can be extended by other templates, promoting reusability and a consistent look across your web pages.

Base Template

The base template contains the common structure (like headers and footers) that other templates will inherit. It typically includes blocks that child templates can override.

Block Tags

Block tags are used within the base template to define sections that child templates can override. These are defined using the {% block %} and {% endblock %} tags.

Extending Templates

Child templates use the {% extends %} tag to inherit from the base template. This tag must be the first tag in the template.

Use Cases

Template inheritance is useful for maintaining consistency across pages, reducing code duplication, and making it easier to update the layout of a site.


{% comment "Base Template" %}



    {% block title %}My Site{% endblock %}

Welcome to My Site

{% block content %}{% endblock %}

Footer information here.

Base Template Explanation

This base template defines a basic HTML structure with a header, main content area, and footer. The {% block %} tags allow child templates to insert their own content into the title and main sections.


{% comment "Child Template" %}
{% extends "base.html" %}

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

{% block content %}
    

Home Page Content

This is the content of the home page.

{% endblock %}

Child Template Explanation

This child template extends the base template and provides specific content for the title and main content areas. The use of {% extends %} ensures that the overall layout defined in the base template is preserved.


{% comment "Another Child Template" %}
{% extends "base.html" %}

{% block title %}
    About Us
{% endblock %}

{% block content %}
    

About Us

Information about our company.

{% endblock %}

Another Child Template Explanation

This example shows another child template that extends the same base template but provides different content. This demonstrates how template inheritance can be used to create multiple pages with a consistent layout.


{% comment "Nested Blocks" %}
{% extends "base.html" %}

{% block content %}
    

Services

Details about our services.

{% block extra_content %} {% endblock %} {% endblock %}

Nested Blocks Explanation

In this example, the child template defines a nested block extra_content within the content block. This allows further extension by other templates, showcasing the flexibility of template inheritance.


{% comment "Extending Nested Blocks" %}
{% extends "services.html" %}

{% block extra_content %}
    

Additional service details here.

{% endblock %}

Extending Nested Blocks Explanation

This template extends the services.html template and fills in the extra_content block. This demonstrates how nested blocks can be overridden to add more specific content.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025