Django Templates are HTML files used to create the user interface of a Django website. Templates allow us to display HTML pages and dynamic data sent from Django views.
A Django template is usually an HTML file that contains the structure of a webpage. It can also contain dynamic Django template code.
<h1>Welcome to My Website</h1>
Templates are used to separate the presentation layer from Python application logic.
Django templates provide several benefits:
A common approach is to create a templates folder inside a Django app.
myapp/
views.py
models.py
templates/
home.html
The HTML file can then be rendered from a view.
Create a file named home.html:
<!DOCTYPE html>
<html>
<head>
<title>Home</title>
</head>
<body>
<h1>Welcome to Django</h1>
</body>
</html>
Django provides the render() function to return an HTML template.
from django.shortcuts import render
def home(request):
return render(request, 'home.html')
The render() function usually receives the request object and the
name of the template.
return render(request, 'home.html')
It creates an HTTP response containing the rendered template.
Django needs to know where templates are located. Template configuration is
usually handled in settings.py.
TEMPLATES = [
{
'BACKEND':
'django.template.backends.django.DjangoTemplates',
'DIRS': [],
'APP_DIRS': True,
},
]
When APP_DIRS is set to True, Django looks for templates
inside the templates directory of installed applications.
'APP_DIRS': True
This is a common setup for app-level templates.
project/
│
├── manage.py
│
├── myproject/
│
└── myapp/
├── views.py
├── models.py
└── templates/
└── home.html
The template can be rendered from a view in the application.
Template variables allow us to display values passed from a view.
<h1>Hello {{ name }}</h1>
Double curly braces {{ }} are used for displaying variables.
def home(request):
data = {
'name': 'Rahul'
}
return render(
request,
'home.html',
data
)
The template can access the name value.
View:
def home(request):
return render(
request,
'home.html',
{'name': 'Amit'}
)
Template:
<h1>Welcome {{ name }}</h1>
The browser will display the value passed by the view.
def profile(request):
data = {
'name': 'Rahul',
'age': 21,
'city': 'Patna'
}
return render(
request,
'profile.html',
data
)
The template can display all these values.
Django templates can access attributes or dictionary values using dot notation.
{{ student.name }}
{{ student.age }}
{{ student.email }}
This is useful when displaying object or dictionary data.
Template tags allow us to perform logic or special operations inside templates.
{% if user %}
<p>Welcome</p>
{% endif %}
Template tags are written using {% %}.
{% if age >= 18 %}
<p>You are an adult.</p>
{% else %}
<p>You are a minor.</p>
{% endif %}
Django templates support conditional rendering using template tags.
The for tag can be used to display multiple items.
{% for student in students %}
<p>{{ student.name }}</p>
{% endfor %}
Filters modify or format values before they are displayed.
{{ name|upper }}
The upper filter converts text to uppercase.
{{ name|lower }}
The lower filter converts text to lowercase.
CSS, JavaScript, and images are commonly managed as static files in Django.
{% load static %}
<link rel="stylesheet"
href="{% static 'css/style.css' %}">
The static template tag generates the appropriate static file URL.
Django provides template comment syntax for comments that should not be rendered.
{# This is a template comment #}
For multi-line comments, Django also provides the comment template tag.
A Django template can contain normal HTML together with Django template syntax.
<h1>{{ title }}</h1>
<p>Welcome to our website.</p>
{% if logged_in %}
<p>You are logged in.</p>
{% endif %}
Browser
↓
URL
↓
View
↓
Template
↓
HTML Response
↓
Browser
The view prepares data and the template is responsible for presenting that data.
View:
def students(request):
students = [
'Rahul',
'Amit',
'Priya'
]
return render(
request,
'students.html',
{'students': students}
)
Template:
{% for student in students %}
<p>{{ student }}</p>
{% endfor %}
Templates can display records retrieved from a Django model.
def students(request):
students = Student.objects.all()
return render(
request,
'students.html',
{'students': students}
)
The template can loop through the returned records.
A base template can contain common website elements such as the header, navigation, and footer.
<!DOCTYPE html>
<html>
<body>
<header>My Website</header>
{% block content %}
{% endblock %}
<footer>Copyright</footer>
</body>
</html>
Another template can reuse a base template with the extends tag.
{% extends 'base.html' %}
{% block content %}
<h1>Home Page</h1>
{% endblock %}
This helps avoid repeating the same HTML structure.
Template inheritance allows multiple pages to share a common layout.
This makes large websites easier to maintain.
APP_DIRS configuration{% load static %} when neededviews.py
from django.shortcuts import render
def home(request):
data = {
'title': 'Django Home',
'name': 'Rahul'
}
return render(
request,
'home.html',
data
)
home.html
<!DOCTYPE html>
<html>
<head>
<title>{{ title }}</title>
</head>
<body>
<h1>Welcome {{ name }}</h1>
</body>
</html>
Django templates provide a powerful way to create dynamic HTML pages. They work together with views to display application data to users.
render() is used to return templates.{{ }}.{% %}.{{ variable }} displays a value.{% tag %} performs template operations.render() connects a view with a template.Question: Which function is commonly used to render a Django template?