Django Base HTML Template Example

Ann Jul 09, 2026

Django, a high-level Python web framework, offers a powerful templating engine that allows developers to create reusable and maintainable HTML templates. The base HTML template in Django serves as a foundation for all other templates, providing a consistent structure and shared elements. Let's explore an example of a base HTML template in Django.

Basic CRUD Structure in Django
Basic CRUD Structure in Django

Before diving into the code, it's essential to understand that Django's templating engine uses a specific syntax for including and extending templates. The base template, often named 'base.html', is typically located in a 'templates' directory within your Django app. Now, let's create a simple base HTML template in Django.

14 Django Website Template | Online Training Download app from below link
14 Django Website Template | Online Training Download app from below link

Setting Up the Base Template

To create a base template, start by creating a new file named 'base.html' in your app's 'templates' directory. This file will contain the shared elements and structure that will be inherited by all other templates in your Django project.

πŸ”— Django Models: OneToOneField (One-to-One Relationship)
πŸ”— Django Models: OneToOneField (One-to-One Relationship)

For instance, you might want to include the site's header, navigation menu, and footer in your base template. These elements will remain consistent across all pages, making your website visually cohesive.

Defining the Base Template Structure

the diagram shows how to use django
the diagram shows how to use django

Here's a basic example of what your 'base.html' file might look like:

<!DOCTYPE html> <html> <head> <title>My Django Site</title> </head> <body> <header> <nav> <ul> <li><a href="/">Home</a></li> <li><a href="/about/">About</a></li> </ul> </nav> </header> <main> <div class="content"> <!-- This is where the content of other templates will be inserted --> </div> </main> <footer> <p>Copyright © 2022 My Django Site</p> </footer> </body> </html>

In this example, the 'content' div is where the dynamic content from other templates will be inserted.

17 Django Create Dynamic Website Template | Online Training Download app from below link
17 Django Create Dynamic Website Template | Online Training Download app from below link

Inheriting the Base Template

Now that we have our base template, we can create other templates that inherit from it. For instance, let's create a 'home.html' template in the same 'templates' directory:

<{% extends 'base.html' %}> <{% block content %}> <h1>Welcome to My Django Site</h1> <p>This is the home page of our Django site.</p> <{% endblock %}>

Django: MVT(Model-View-Template)
Django: MVT(Model-View-Template)

In this 'home.html' template, we're using Django's template language to extend the 'base.html' template and define the content that will be inserted into the 'content' div.

Using Variables and Templates Tags

a diagram with the words django in different languages
a diagram with the words django in different languages
πŸ“„ Template with Model Relations in Django
πŸ“„ Template with Model Relations in Django
βš™οΈ Django Generic Views – Build Faster with Less Code
βš™οΈ Django Generic Views – Build Faster with Less Code
Django Insert Data Into Database with Source Code
Django Insert Data Into Database with Source Code
Django vs Flask: Which Python Framework Should You Learn First?
Django vs Flask: Which Python Framework Should You Learn First?
Django Views
Django Views
Simple Introduction to Django Forms (Django Tutorial) | Part 45
Simple Introduction to Django Forms (Django Tutorial) | Part 45
Secure Your Django Project with .env Files
Secure Your Django Project with .env Files
Django Models – choices Option
Django Models – choices Option
Django Models – Field Options Explained
Django Models – Field Options Explained
Basic Attributes of Django Generic Views
Basic Attributes of Django Generic Views
πŸ—„οΈ Create a Database in Django
πŸ—„οΈ Create a Database in Django
Start a Django Project
Start a Django Project
πŸ”— Django Models – Relationship Fields
πŸ”— Django Models – Relationship Fields
Create Django Models
Create Django Models
Gym Management System Project in Django with Source Code
Gym Management System Project in Django with Source Code
Add Google Login to Your Django App with Allauth
Add Google Login to Your Django App with Allauth
Django Quick Start Guide – Build Web Apps Faster!
Django Quick Start Guide – Build Web Apps Faster!
πŸ“˜ Django Models: Data Field Types
πŸ“˜ Django Models: Data Field Types
Python Django for Web Apps: Beginner’s Overview
Python Django for Web Apps: Beginner’s Overview

Django's templating engine also allows you to use variables and template tags to generate dynamic content. For example, you can pass variables from your view to your template and display their values:

<{% extends 'base.html' %}> <{% block content %}> <h1>Welcome, {{ user.name }}!</h1> <p>This is the home page of our Django site.</p> <{% endblock %}>

In this example, 'user.name' is a variable passed from the view to the template, and its value will be displayed in the 'h1' tag.

Including Other Templates

Django also provides a template tag called 'include' that allows you to include the content of one template within another. This can be useful for including common elements like a sidebar or a footer in multiple templates:

<{% extends 'base.html' %}> <{% block content %}> <h1>Welcome to My Django Site</h1> <p>This is the home page of our Django site.</p> <{% include 'sidebar.html' %}> <{% endblock %}>

In this example, the content of 'sidebar.html' will be included in the 'home.html' template.

By using a base HTML template in Django, you can create a consistent and maintainable structure for your website. This approach promotes code reuse, simplifies template creation, and makes your project more organized. Happy coding!