What are Templates & Inheritance in Django?

Part 3: Django Series

Anurag
4 min readFeb 5, 2023

In this article series on Django, we will learn what are Templates & Inheritance in Django.

Django is a powerful web framework for Python that makes it easy to build web applications quickly. One of the key features of Django is its support for templates and inheritance, which allow you to create reusable, modular code that is easy to maintain and scale.

In Django, templates are used to define the structure of a webpage or a section of a webpage. They are written in HTML and can include special tags that Django will interpret and replace with dynamic content when the page is loaded. Here’s an example of a simple Django template that displays a list of blog posts:

<h1>My Blog</h1>

{% for post in posts %}
<h2>{{ post.title }}</h2>
<p>{{ post.body }}</p>
{% endfor %}

The {% for %} and {% endfor %} tags define a loop that will iterate over the posts object and display the title and body of each post. The {{ }} tags are used to display the values of variables in the template.

One of the key benefits of using templates is that they allow you to separate the design and layout of your webpage from the underlying Python code that powers it. This makes it easy to make changes to the look and feel of your site without having to touch any of the code.

Inheritance is another important concept in Django templates. It allows you to create a base template that defines the overall structure of your site, and then derive other templates from it that override or extend the base template as needed. This allows you to create a consistent look and feel across your site, while still allowing for flexibility and customization.

To use inheritance in Django, you create a base template that includes special blocks of code where you want child templates to override or extend the base template. You can then create a child template that extends the base template and overrides any of the blocks as needed.

For example, you might create a base template that defines the overall structure of your site, including the header, footer, and navigation. You could then create a child template for a specific page that extends the base template and overrides the content block to include the specific content for that page.

Here’s an example of a base template that defines a header and footer, with a content block in between:

<html>
<head>
<title>My Site</title>
</head>
<body>
<header>
<h1>My Site</h1>
<nav>
<ul>
<li><a href="/">Home</a></li>
<li><a href="/about/">About</a></li>
</ul>
</nav>
</header>
{% block content %}
{% endblock %}
<footer>
<p>Copyright {{ current_year }}</p>
</footer>
</body>
</html>

To use this base template, you would create a child template that extends it and overrides the content block. Here's an example of a child template that extends the base template and displays a list of blog posts:

{% extends "base.html" %}

{% block content %}
<h1>My Blog</h1>

{% for post in posts %}
<h2>{{ post.title }}</h2>
<p>{{ post.body }}</p>
{% endfor %}
{% endblock %}

In this example, the child template extends the `base.html` template and overrides the `content` block. The {% extends %} tag tells Django to use the `base.html` template as the base, and the {% block %} and {% endblock %} tags define the content that will replace the `content` block in the base template.

Using templates and inheritance can greatly simplify the development process in Django, and make it easier to maintain and scale your web applications. Whether you’re building a simple blog or a complex web application, Django’s support for templates and inheritance makes it a powerful and flexible choice for web development.

To conclude, templates and inheritance are important concepts in Django that allow you to create reusable, modular code and maintain a consistent look and feel across your website. By separating the design and layout of your website from the underlying Python code, you can make changes to the appearance of your site without having to touch the code. And by using inheritance, you can create a base template that defines your site’s overall structure and create child templates that override or extend the base template as needed. Whether you’re a beginner or an experienced developer, learning how to use templates and inheritance will help you create powerful and effective web applications with Django.

Hope you get some insights into Django from this article. See you again in another blog.

--

--

Anurag

Currently working as Product Manager who is also a passionate engineer with an experience in Artificial Intelligence.