Django Templates
In the previous lesson, you saw that a view can return HTML — but writing HTML inside Python code is messy and hard to manage. Django solves this problem with Templates. A template is a separate HTML file where you design your page, and Django fills in the real data before sending it to the browser.
What is a Template?
A Template is an HTML file with special placeholders. These placeholders are replaced with actual data by Django when the page is requested. This keeps your HTML design completely separate from your Python logic.
Example: Imagine a birthday invitation card template. The design (balloons, borders, fonts) is already printed. You just fill in the blanks — the guest's name, the date, the venue. Django templates work exactly the same way. The design is ready; Django fills in the real data.
Setting Up the Templates Folder
Django looks for templates in a specific folder. You need to create a templates folder and tell Django where to find it.
Option 1 — App-level templates folder (recommended for beginners):
Create a templates folder inside your app folder, then inside it create another folder with the app name:
students/
└── templates/
└── students/
└── home.html <-- Your template file
Option 2 — Project-level templates folder:
Create a templates folder at the root of your project and configure it in settings.py:
myschool/
├── templates/
│ └── home.html
├── manage.py
└── myschool/
Then in settings.py, update the TEMPLATES setting to tell Django where to look:
TEMPLATES = [
{
...
'DIRS': [BASE_DIR / 'templates'], # <-- Add this line
...
},
]
Creating Your First Template
Create a file named home.html inside your templates folder and write a simple HTML page:
<!-- students/templates/students/home.html -->
<!DOCTYPE html>
<html>
<head>
<title>Students Portal</title>
</head>
<body>
<h1>Welcome, {{ name }}!</h1>
<p>Your total marks are: {{ marks }}</p>
</body>
</html>
The {{ name }} and {{ marks }} are template variables. Django replaces these with actual values from the context dictionary passed by the view.
Connecting the View to the Template
In your view, use the render() function to load the template and pass data to it:
# students/views.py
from django.shortcuts import render
def home(request):
context = {
'name': 'Priya Verma',
'marks': 465,
}
return render(request, 'students/home.html', context)
When the user visits the page, Django finds home.html, replaces {{ name }} with "Priya Verma" and {{ marks }} with 465, and sends the final page to the browser.
Template Variables
Template variables display data passed from the view. They are always written inside double curly braces:
{{ variable_name }}
You can also access properties of objects and items in lists using dot notation:
{{ student.name }} <-- Access the 'name' property of a student object
{{ student.marks }} <-- Access the 'marks' property
{{ subjects.0 }} <-- Access the first item in a list
Example: Template variables are like name tags at a conference. The tag template (design) is the same for everyone. You just stick a different name on each one. {{ name }} is the blank space on the tag.Template Inheritance — The Base Template
Most websites have a common layout — the same header, navigation bar, and footer on every page. Instead of copying this layout into every HTML file, Django lets you create a base template that all other pages can inherit from.
Create a base.html file as the master layout:
<!-- templates/base.html -->
<!DOCTYPE html>
<html>
<head>
<title>My School</title>
</head>
<body>
<header>
<h1>My School Website</h1>
<nav>Home | Students | Teachers | Contact</nav>
</header>
{% block content %}
<!-- Child pages will place their content here -->
{% endblock %}
<footer>
<p>Copyright 2024 My School</p>
</footer>
</body>
</html>
Now create a child template that extends this base:
<!-- students/templates/students/home.html -->
{% extends 'base.html' %}
{% block content %}
<h2>Welcome, {{ name }}!</h2>
<p>Your total marks are: {{ marks }}</p>
{% endblock %}
Example: Template inheritance is like a printed newspaper layout. The header, columns, and footer design is fixed (base template). Each journalist just fills in their specific article content (child template block). The layout stays consistent without anyone copying it again and again.
Including a Template Inside Another Template
You can break your page into smaller reusable pieces using the {% include %} tag. For example, a navigation bar that appears on multiple pages can be saved as a separate file:
<!-- templates/navbar.html -->
<nav>
<a href="/">Home</a> |
<a href="/students/">Students</a> |
<a href="/contact/">Contact</a>
</nav>
Then include it in your base template:
{% include 'navbar.html' %}
Quick Recap
- Templates are HTML files with placeholders that Django fills with real data.
- Use
{{ variable }}to display values passed from the view. - Store templates in a
templates/folder inside your app or project root. - Use
{% extends 'base.html' %}to inherit a common layout. - Use
{% block content %}...{% endblock %}to define changeable areas in the layout. - Use
{% include 'file.html' %}to insert reusable HTML pieces into a template.
