Django: Extend Templates with Base.html

Ann Jul 09, 2026

In the realm of web development, particularly when using the Django framework, templates play a pivotal role in structuring and presenting our web applications' content. One powerful feature of Django's template system is the ability to extend base templates, promoting code reuse and maintaining a consistent layout across your website.

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

Django's template inheritance, facilitated by the 'extends' tag, allows developers to create a base template (often referred to as the 'base.html' file) that defines the overall structure and common elements of a website. This base template can then be extended by other templates, which can override or add to the base's content, leading to a clean, organized, and efficient codebase.

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

Understanding Django's Template Inheritance

Django's template inheritance is a top-down approach, meaning that child templates (those extending the base) inherit variables and blocks from their parent template. This allows for a clear separation of concerns, with the base template handling the overall layout and structure, and child templates focusing on the unique content for each page.

Customize Views: Add Extra Context in Django
Customize Views: Add Extra Context in Django

To illustrate, consider a simple base template with the following structure:

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

{% block content %} {% endblock %}
```

Blocks and Block Content

🗑️ Template for Delete Page in Django
🗑️ Template for Delete Page in Django

In the above example, the 'title' and 'content' blocks are defined using the 'block' template tag. These blocks serve as placeholders that can be overridden by child templates. The content within these blocks is replaced by the content provided in the child template.

Here's how you might extend this base template in a child template, say 'index.html':

```html {% extends 'base.html' %} {% block title %}Home - My Website{% endblock %} {% block content %}

Welcome to my website!

Basic CRUD Structure in Django
Basic CRUD Structure in Django

This is the home page of my website.

{% endblock %} ```

Inheriting Variables

Besides blocks, Django also allows child templates to inherit variables from their parent template. This can be achieved using the 'context' variable in the base template and accessing it in the child template. Here's an example:

Understanding Context in Django Templates
Understanding Context in Django Templates

In 'base.html':

```html {% context my_variable = "Hello, World!" %} ```

And in 'index.html':

Template for List Page in Django
Template for List Page in Django
Create Views in Django
Create Views in Django
🔗 Django Models: OneToOneField (One-to-One Relationship)
🔗 Django Models: OneToOneField (One-to-One Relationship)
Add Links with the {% url %} Tag in Django Templates
Add Links with the {% url %} Tag in Django Templates
📊 Django Templates vs Django APIs: Key Differences
📊 Django Templates vs Django APIs: Key Differences
Django Project Ideas for Beginners to Experts
Django Project Ideas for Beginners to Experts
the diagram shows how to use django
the diagram shows how to use django
Why Django Framework is Best For Web Development?
Why Django Framework is Best For Web Development?
14 Django Website Template | Online Training Download app from below link
14 Django Website Template | Online Training Download app from below link
Django: MVT(Model-View-Template)
Django: MVT(Model-View-Template)
Django custom 500 error template page
Django custom 500 error template page
📄 Template with Model Relations in Django
📄 Template with Model Relations in Django
Customizing Views: Filter Lists in Django Admin
Customizing Views: Filter Lists in Django Admin
Django Bootstrap Base Template
Django Bootstrap Base Template
Secure Your Django Project with .env Files
Secure Your Django Project with .env Files
Understanding Django's MVT Framework
Understanding Django's MVT Framework
Django Insert Data Into Database with Source Code
Django Insert Data Into Database with Source Code
Django Deployment Simplified
Django Deployment Simplified
Django Views
Django Views
Django Ecommerce with Source Code
Django Ecommerce with Source Code

```html {% extends 'base.html' %} {% block content %}

{{ my_variable }}

{% endblock %} ```

Benefits of Extending Base Templates

Using Django's template inheritance brings several benefits to your web development process:

  • Code Reuse: Common elements like headers, footers, and navigation menus can be defined in the base template, reducing duplication across your templates.
  • Consistency: By maintaining a consistent layout across your website, users will have a familiar experience as they navigate through your application.
  • Efficiency: Changes to the overall layout can be made in one place (the base template), rather than in every individual template.

In conclusion, Django's template inheritance, facilitated by the 'extends' tag, is a powerful tool for creating efficient, consistent, and maintainable web templates. By leveraging this feature, developers can streamline their workflow and create robust, user-friendly web applications.

Now that you've understood the basics of extending base templates in Django, why not give it a try in your next project? Start with a simple base template and extend it to create unique pages for your website. Happy coding!