extends – Django Template Tags

A Django template is a text document or a Python string marked-up using the Django template language. Django being a powerful Batteries included framework provides convenience to rendering data in a template. Django templates not only allow paassing data from view to template, but also provides some limited features of a programming such as variables, for loops, comments, extends etc.
This article revolves about how to use extends tag in Templates. extends tag is used for inheritance of templates in django. One needs to repeat the same code again and again. Using extends we can inherit templates as well as variables.

Syntax

{% extends 'template_name.html' %}

Example

assume the following directory structure:

dir1/
    template.html
    base2.html
    my/
        base3.html
base1.html

In template.html, the following paths would be valid:

filter_none

edit
close

play_arrow

link
brightness_4
code

{% extends "./base2.html" %}
{% extends "../base1.html" %}
{% extends "./my/base3.html" %}

chevron_right


extends – Django template Tags Explanation

Illustration of How to use extends tag in Django templates using an Example. Consider a project named geeksforgeeks having an app named geeks.

Refer to the following articles to check how to create a project and an app in Django.



Now create a view through which we will access the template,
In geeks/views.py,

filter_none

edit
close

play_arrow

link
brightness_4
code

# import Http Response from django
from django.shortcuts import render
  
# create a function
def geeks_view(request):
  
    # return response
    return render(request, "extendedgeeks.html")

chevron_right


Create a url path to map to this view. In geeks/urls.py,

filter_none

edit
close

play_arrow

link
brightness_4
code

from django.urls import path
  
# importing views from views.py
from .views import geeks_view
  
urlpatterns = [
    path('', geeks_view),
]

chevron_right


extends is always used with block tags so that can be inherited and overriden. Create a template as base template in templates/geeks.html.

filter_none

edit
close

play_arrow

link
brightness_4
code

<h1>Main Template</h1>
  
{% block content %}
{% endblock %}

chevron_right


Now create a template which will use geeks.html as base template. Create a new template extendedgeeks.html,

filter_none

edit
close

play_arrow

link
brightness_4
code

{% extends "geeks.html" %}
  
{% block content %}
<h2> GeeksForGeeks is the Best
{% endblock %}

chevron_right


Let’s check if data is displayed from both the templates in extendedgeeks.html
extends-django-templates-tags

Advanced Usage

{% extends variable %} uses the value of variable. If the variable evaluates to a string, Django will use that string as the name of the parent template. If the variable evaluates to a Template object, Django will use that object as the parent template.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.