Django template variables are used to display dynamic data inside HTML templates.
Data can be sent from a Django view to a template and then displayed using
double curly braces such as {{ name }}.
A template variable is a value that is passed from a Django view and displayed inside an HTML template.
<h1>{{ name }}</h1>
Here, name is a template variable.
Template variables make HTML pages dynamic instead of displaying only fixed text.
Django uses double curly braces to display a variable.
{{ name }}
If the variable contains Rahul, Django displays:
Rahul
from django.shortcuts import render
def home(request):
data = {
'name': 'Rahul'
}
return render(
request,
'home.html',
data
)
The dictionary is used to send the name value to the template.
home.html
<h1>Welcome {{ name }}</h1>
If the view sends Rahul, the page displays:
Welcome Rahul
A context dictionary contains the data that is passed from the view to the template.
data = {
'name': 'Rahul',
'age': 21
}
Both values can be accessed inside the template.
View:
def profile(request):
data = {
'name': 'Amit',
'age': 22,
'city': 'Patna'
}
return render(
request,
'profile.html',
data
)
Template:
<h2>{{ name }}</h2>
<p>Age: {{ age }}</p>
<p>City: {{ city }}</p>
A template variable can contain a string value.
data = {
'name': 'Rahul'
}
Template:
<p>Name: {{ name }}</p>
Template variables can also contain numbers.
data = {
'age': 25
}
Template:
<p>Age: {{ age }}</p>
Numeric values such as prices can also be passed to templates.
data = {
'price': 499.50
}
Template:
<p>Price: ₹{{ price }}</p>
A Boolean value can also be passed to a template.
data = {
'is_student': True
}
Boolean values are commonly used with conditional template tags.
{% if is_student %}
<p>This user is a student.</p>
{% endif %}
A view can send a list to a template.
data = {
'subjects': [
'Python',
'Django',
'SQL'
]
}
The list can be displayed using a for loop.
{% for subject in subjects %}
<p>{{ subject }}</p>
{% endfor %}
The loop displays every item from the subjects list.
A dictionary can also be passed from a view to a template.
data = {
'student': {
'name': 'Rahul',
'age': 20,
'city': 'Patna'
}
}
Django templates can use dot notation to access dictionary values.
{{ student.name }}
{{ student.age }}
{{ student.city }}
This is commonly used when displaying structured data.
A Django model object can also be passed to a template.
def student(request):
student = Student.objects.get(id=1)
return render(
request,
'student.html',
{'student': student}
)
Object attributes can be displayed using dot notation.
<h1>{{ student.name }}</h1>
<p>{{ student.email }}</p>
<p>{{ student.mobile }}</p>
This is useful for displaying database records.
{% if name %}
<h2>Hello {{ name }}</h2>
{% else %}
<h2>Name not available</h2>
{% endif %}
A condition can check whether a value is available.
The default filter can provide a value when a variable is empty or unavailable.
{{ name|default:"Guest" }}
If name does not contain a usable value, Django displays
Guest.
Filters can modify the value of a template variable.
{{ name|upper }}
For example, rahul can be displayed as RAHUL.
{{ name|lower }}
The lower filter converts text to lowercase.
A template variable can also be used inside an HTML attribute.
<img src="{{ image_url }}" alt="{{ name }}">
The actual values are supplied by the Django view.
<a href="{{ website }}">
Visit Website
</a>
The website variable can contain the destination URL.
Views often retrieve multiple records from a database and pass them to templates.
def students(request):
students = Student.objects.all()
return render(
request,
'students.html',
{'students': students}
)
The template can then display the records using a loop.
{% for student in students %}
<h3>{{ student.name }}</h3>
<p>{{ student.email }}</p>
{% endfor %}
Each database record can be displayed using its attributes.
Variables can contain nested dictionaries or objects.
data = {
'student': {
'name': 'Amit',
'course': 'Python',
'city': 'Patna'
}
}
Template:
{{ student.name }}
{{ student.course }}
{{ student.city }}
If a variable is not available in the template context, Django generally renders an empty string when the variable is accessed directly.
<p>{{ unknown_variable }}</p>
It is therefore important to use the correct variable names in both the view and template.
Use meaningful names for template variables.
student_name
course_name
product_price
user_email
Meaningful names make templates easier to understand and maintain.
Correct:
{{ name }}
Incorrect:
{ name }
views.py
from django.shortcuts import render
def profile(request):
data = {
'name': 'Rahul',
'age': 21,
'course': 'Django',
'city': 'Patna'
}
return render(
request,
'profile.html',
data
)
profile.html
<h1>Student Profile</h1>
<p>Name: {{ name }}</p>
<p>Age: {{ age }}</p>
<p>Course: {{ course }}</p>
<p>City: {{ city }}</p>
Template variables are an important part of Django's template system. They allow a view to send dynamic information to an HTML page.
{{ variable }}.{{ variable }} to display a value.for loops.Question: Which syntax is used to display a variable in a Django template?