extends - Django Template Tags
Last Updated :
20 May, 2025
Django’s {% extends %} tag allows you to reuse a base template across multiple pages, so you don’t have to repeat the same HTML code in every template. This makes your templates cleaner, easier to maintain, and helps keep a consistent layout throughout your site.
Syntax:
{% extends 'base_template.html' %}
Example: Assume the following directory structure:
project_root/
templates/
base.html
app/
child.html
You would extend the base template in child.html like this:
{% extends "base.html" %}
or, if the base is inside the app folder:
{% extends "app/base.html" %}
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.
Example Project Structure:
myproject/
templates/
geeks.html # Base template
extendedgeeks.html # Child template extending geeks.html
myapp/
views.py
urls.py
Step 1: Create a Base Template (geeks.html)
This template defines the main structure of your page with a content block that child templates can override.
HTML
<!DOCTYPE html>
<html>
<head>
<title>My Site</title>
</head>
<body>
<h1>Main Template Header</h1>
{% block content %}
<!-- Default content can go here -->
{% endblock %}
<footer>
<p>Footer information here</p>
</footer>
</body>
</html>
Step 2: Create a Child Template (extendedgeeks.html)
This template extends geeks.html and overrides the content block to provide page-specific content.
HTML
{% extends "geeks.html" %}
{% block content %}
<h2>GeeksForGeeks is the Best</h2>
<p>Welcome to the extended template example!</p>
{% endblock %}
Step 3: Define a View to Render the Template (views.py)
In your Django app’s views.py:
Python
from django.shortcuts import render
def geeks_view(request):
return render(request, "extendedgeeks.html")
Add the URL path to access your view in urls.py:
Python
from django.urls import path
from .views import geeks_view
urlpatterns = [
path('', geeks_view, name='geeks_home'),
]
Step 5: Run the Development Server
Make sure your virtual environment is activated, then run:
python manage.py runserver
Let's check if data is displayed from both the templates in extendedgeeks.html by visiting URL- http://127.0.0.1:8000/ in your browser, you should see:
- The header from geeks.html
- The content from extendedgeeks.html
- The footer from geeks.html

Note: {% 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.
Also Read: Template Filters
Similar Reads
Django Templates Templates are the third and most important part of Django's MVT Structure. A Django template is basically an HTML file that can also include CSS and JavaScript. The Django framework uses these templates to dynamically generate web pages that users interact with. Since Django primarily handles the ba
7 min read
variables - Django Templates Prerequisite- Django TemplatesIn Django, templates are used to dynamically generate HTML content by combining static HTML with dynamic data from views. One of the simplest and most useful features of Django templates is the use of variables. Variables allow you to display data passed from a view ins
2 min read
Django Template Tags Prerequisite: What are Django Templates?Django provides a powerful templating engine that allows us to add logic directly into our templates using template tags. These tags enable everything from control structures (like if and for loops), to inserting dynamic content, to template inheritance. Templ
4 min read
extends - Django Template Tags Djangoâs {% extends %} tag allows you to reuse a base template across multiple pages, so you donât have to repeat the same HTML code in every template. This makes your templates cleaner, easier to maintain, and helps keep a consistent layout throughout your site.Syntax: {% extends 'base_template.htm
2 min read
if - Django Template Tags The {% if %} tag in Django templates allows us to control what content is displayed based on certain conditions. We can use it to show or hide parts of a page depending on whether a condition is met.Syntax of the {% if %} Tag{% if variable %}// statements{% else %}// statements{% endif %}condition:
3 min read
for loop - Django Template Tags Django templates allow you to render dynamic data by embedding Python-like logic into HTML. The for loop is one of the most commonly used template tags, enabling you to iterate over lists or other iterable objects. It helps you display repeated content (like lists, tables, etc.) in your templates wi
3 min read
comment - 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 passing data from view to the template but also provide som
2 min read
include - 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 passing data from view to template, but also provides some
2 min read
url - Django Template Tag 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 passing data from view to template, but also provides some
3 min read
cycle - 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 passing data from view to template, but also provides some
3 min read