Skip to content

1. Basics

Templates in Django are used to separate HTML presentation logic from the backend Python logic. They allow you to dynamically generate web pages using Django’s template language.

Django templates:

  • Contain HTML with Django Template Language (DTL) (e.g., {% for %}, {% if %}, {{ variable }}).
  • Help render dynamic content from views.
  • Allow reuse and modularization using base templates.

Inside your app directory (e.g., blog), create a templates/ folder:

blog/
├── templates/
│ ├── blog/
│ │ ├── post_list.html

Now, define an HTML file inside templates/blog/post_list.html:

<!DOCTYPE html>
<html>
<head>
<title>Blog Posts</title>
</head>
<body>
<h1>Blog Posts</h1>
{% for post in posts %}
<h2>{{ post.title }}</h2>
<p>{{ post.content }}</p>
{% endfor %}
</body>
</html>

In views.py, use render() to return an HTML page:

from django.shortcuts import render
from .models import Post
def post_list(request):
posts = Post.objects.all() # Fetch all posts
return render(request, 'blog/post_list.html', {'posts': posts})

3. Defining TEMPLATES Directory in settings.py

Section titled “3. Defining TEMPLATES Directory in settings.py”

By default, Django looks for templates inside each app’s templates/ folder. However, you can define a global templates/ folder in your project.

Step 1: Create a Global templates/ Directory

Section titled “Step 1: Create a Global templates/ Directory”

Create a templates/ folder in the project’s root directory:

myproject/
├── templates/
│ ├── base.html

Tell Django to look in the global templates/ directory:

import os
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR, 'templates')], # Add this line
'APP_DIRS': True,
'OPTIONS': {
'context_processors': [
'django.template.context_processors.debug',
'django.template.context_processors.request',
'django.contrib.auth.context_processors.auth',
'django.contrib.messages.context_processors.messages',
],
},
},
]
  • DIRS: Specifies additional directories where Django looks for templates.
  • APP_DIRS: Enables Django to look inside each app’s templates/ folder.

Define a common layout in templates/base.html:

<!DOCTYPE html>
<html>
<head>
<title>{% block title %}My Website{% endblock %}</title>
</head>
<body>
<header>
<h1>My Blog</h1>
<nav>
<a href="/">Home</a>
<a href="/about/">About</a>
</nav>
</header>
<main>
{% block content %}{% endblock %}
</main>
<footer>
<p>&copy; 2024 My Blog</p>
</footer>
</body>
</html>

Step 2: Extend base.html in Other Templates

Section titled “Step 2: Extend base.html in Other Templates”

Now, use {% extends "base.html" %} in child templates.

{% extends "base.html" %}
{% block title %}Blog Posts{% endblock %}
{% block content %}
<h1>Blog Posts</h1>
{% for post in posts %}
<h2>{{ post.title }}</h2>
<p>{{ post.content }}</p>
{% endfor %}
{% endblock %}

Reduces Repetition – Define headers, footers, and navigation only once.
Easy Maintenance – Change layout in base.html, and all pages update automatically.
Consistent Design – Ensures a unified look across all pages.