Search Here

Django Template Inheritance using extends and include template tags

For displaying the HTML output Django has a language which is called Django Template Language and it’s very easy to learn and understand. The main feature of the Django Template Language is inheritance and reusability to templates.
Django has a special template tag that can be used to inherit the contents of one template and reuse it else were. Also, you can create sections of templates in separate HTML files and can use them whenever needed hence greatly reducing the development time.

In this post, you’ll learn how to use the extends and include template tags for inheritance and reusability.

Using extends template tag

The extends is a template tag that can inherit templates from the parent HTML template. Using templates in projects which greatly reduce the HTML size and the same elements once provided in the base file can be used as a skeleton to render contents on the browser.

Here is an HTML template in main_app\templates\main_app\base.html

        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1">

    {% block menus %}
    {% endblock %}

    {% block content %}

    {% endblock %}

    {% block scripts %}
            let users = ["ravi", "shivu"];

    {% endblock %}

And to import this template into other template files use {% extends ‘main_app/base.html’ %} template tag.
In app\templates\app\list.html

{% extends 'main_app/base.html' %}

{% block menus %}
    {% include 'main_app/menus.html' with name="The Code Learners" %}
{% endblock %}

{% block content %}
    <div class="text-center">
        <h1>My First Django Page</h1>
        <p>Resize this responsive page to see the effect!</p>
{% endblock %}

{% block scripts %}
    var name = "django";
{% endblock %}

The {% block content %} tags act as a container that holds HTML code. You can create any number of blocks can specify the name of the block. But for each block you create, it must always have {% endblock %}.

{% block any_name %}
    <h2>HTML Content goes within this block</h2>
{% endblock %}

{% block footer %}

{% endblock %}

You can also override the HTML template within the block and also have the option to include the previous HTML contents of the block using {{block.super}}. Which will keep the HTML contents from getting removed.

Since we have created a template in main_app which is a default Django app. So adding template inside the template folder will not be rendered by Django so to fix this we need to tell Django were to look at this template and that is by going to the file and updating DIRS inside the TEMPLATES list.

        'DIRS': [
            'main_app/templates/' #only add this one

No restart your Django Server and next the Django will also search within the main_app/templates/ when rendering.

Using include template tag

The include will output the data from the template which is outside the HTML file. This template tag exists mainly for the purpose of reusability.


{% include "template_path" %}
{% include "template_path" with var_name="value" %}

Using with the keyword you can also pass the values to the template.


In main_app\templates\main_app\menus.html

<a href="#">{{ var_name }}</a>
<ul >
        <a href="#">Home</a>
        <a href="#">Services</a>

And to include this template use the below example.

{% include 'main_app/menus.html' with var_name="The Code Learners" %}

Watch Video