Table of contents
1.
Introduction
2.
Tags in Django Templates
2.1.
extends Tag in Django Template
2.1.1.
Syntax
2.1.2.
Requirements for using extends tag
2.1.3.
Example with explanation
3.
Frequently Asked Questions
4.
Key Takeaways
Last Updated: Mar 27, 2024

Extends Tag in Django Template

Author Tanay kumar Deo
2 upvotes
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Django is one of Python's most popular, trusted, and reliable web development frameworks. With Django, we can quickly create web applications without worrying about installation or dependency problems that we usually find with other frameworks.

Django provides convenience to rendering data in a template. Django templates allow the passing of data from view to template and provide some limited programming features such as variables, for loops, etc. Django Template Tags are Python functions that accept one or more values and optional arguments, process those values, and return a value displayed on the page.

In this article, we will learn about extends tag in Django Template. The extends tag in Django template is used for inheritance. We can inherit templates as well as variables with this tag. 

Tags in Django Templates

Tags in Django Templates look like this: {% tag %}. Tags are more complex than variables. Tags can create text in the output, control structure with loops or logic, and load external information into the Django template to be used by later variables.

In Django templates, tags provide arbitrary logic during the rendering process. For example, a tag can output text, serve as a control structure with the help of loops or logic, e.g., a 'for' loop or an 'if' statement. A tag can also grab information from the database.

Given below is an example of the usage of tags in Django templates. The code will print “Hello, {{ user.username }}.”  only if user.is_authenticated is true. {% csrf_token %}, {% if user.is_authenticated %} and {%endif %} are tags while {{ user.username}} is a variable.

{% csrf_token %}  
 
{% if user.is_authenticated %}  
Hello, {{ user.username }}.  
{% endif %} 
You can also try this code with Online Python Compiler
Run Code

 

Let's take our user as logged in with a username of 'Naman'; then the above template will render to:

Hello, Naman.

extends Tag in Django Template

The extends tag in Django Template is used to inherit Django templates. We do not need to repeat the same code again and again. With extends, we can inherit Django templates as well as Django variables.

Syntax

The extends tag can be used in two ways:

  • {% extends "template_name.html" %} (with quotes) uses the literal value "template_name.html" as the name of the parent template to extend.
  • {% extends variable_name %} uses the value of variable_name. If the variable evaluates to a string, the Django template will use that string as the name of the parent template. And if the variable evaluates to a Template object, the Django template will use that object as the parent template.


Normally the template name is a relative path to the template loader's root directory. A string argument may also be a relative path starting with ./ or ../

Requirements for using extends tag

Using the extends tag in Django template requires a few things.

  1. First, we need a parent Django template to extend
  2. Then, we need to add a Django extend {% block content %} tags where other templates will load in.
  3. Finally, we will extend the parent template on the top of the other templates and will add a matching Django extend {% block content %} tags to the other templates

Example with explanation

For example, in this article, we will consider a directory with the following structure:

project_directory/

..

templates/

                  base.html

   home.html

In the above example, we have a home.html template in the root directory, and base.html is located in the root/templates directory.

First of all, we will define the base.html file,

<html>
  <head>
  </head>

  <body>
      <h2>Hey this is base django template</h2>
      <hr />

     {% block content %}
     {% endblock %}
   </body>
</html>
You can also try this code with Online Python Compiler
Run Code

 

With the above code, we can see that base.html is a basic Django template with the body having only a {% block content %} tag.

Now let's make our home.html file, 

{% extends "./templates/base.html" %}
 
{% block content %}
<h3> This is the Home page. </h3>
<p> We are learning from coding Ninjas.</p>
{% endblock %}
You can also try this code with Online Python Compiler
Run Code

 

In the home.html, we used {% extends "./templates/base.html" %} tag to extend base.html as the parent template. We can see that the {% block content %} tag is used in both base.html and home.html. Basically, the block tag is inherited from the base.html and overridden in the home.html template.

Let's check if data is properly displayed from both the templates in home.html,

OUTPUT

 

Frequently Asked Questions

  1. What is the Django Template?
    A Django template is a python string or a text document marked up using the Django template language. It uses its syntax to deal with tags, variables, filters, etc.
     
  2. What is the difference between include and extends tag in Django template?
    The extends tag in Django template allows us to replace some blocks (e.g., "content") from the parent template while Django includes tag is used to include one template in another and render it.

Key Takeaways

In this article, we learned about extends tag in Django template, its usage, and examples. We created a parent Django template base.HTML with {% block content %} tag. Next, we created another Django template home.html and extended the parent template with {% extends "./templates/base.html" %} and then overridden the {% block content %} tag.

Don't stop here. Check out the blogs Best Django BooksTop 30 Basic Django Interview Questions: Part 1, and Top 30 Intermediate Django Interview Questions: Part 2.

We hope you found this blog helpful. Liked the blog? Then feel free to upvote and share it.

Live masterclass