๐ Introduction
In Django templates, we can:
- Pass multiple variables from view
- Access them using template syntax
- Understand how Django resolves variables (lookup mechanism)
๐ Variable lookup follows this order:
- Dictionary
- Object attribute
- List index
๐ฏ Program Statement
๐ Demonstrate multiple context variables and variable lookup inside templates.
โ๏ธ Step 1: Create View
๐ File: views.py
๐น Path:
myproject/myapp/views.py
๐น Code:
from django.shortcuts import render
def demo_context(request):
# Simple variable
name = "Juhi"
# List
marks = [78, 85, 90]
# Dictionary
student = {
"course": "MCA",
"year": "2nd Year"
}
# Nested dictionary (for lookup)
data = {
"student": {
"name": "Aman",
"age": 22
}
}
return render(request, 'demo.html', {
'name': name,
'marks': marks,
'student': student,
'data': data
})
๐ง Explanation
We passed:
- String โ
name - List โ
marks - Dictionary โ
student - Nested dictionary โ
data
โ๏ธ Step 2: URL Mapping
๐ File: urls.py
๐น Path:
myproject/myproject/urls.py
๐น Code:
from django.contrib import admin
from django.urls import path
from myapp import views
urlpatterns = [
path('admin/', admin.site.urls),
path('demo/', views.demo_context),
]
โ๏ธ Step 3: Create Template
๐ File: demo.html
๐น Path:
myproject/templates/demo.html
๐น Code:
<!DOCTYPE html>
<html>
<head>
<title>Context Demo</title>
</head>
<body>
<h1>Multiple Context Variables</h1>
<!-- Simple variable -->
<p>Name: {{ name }}</p>
<hr>
<!-- List access -->
<h3>Marks:</h3>
<p>First Mark: {{ marks.0 }}</p>
<p>Second Mark: {{ marks.1 }}</p>
<ul>
{% for m in marks %}
<li>{{ m }}</li>
{% endfor %}
</ul>
<hr>
<!-- Dictionary access -->
<h3>Student Info:</h3>
<p>Course: {{ student.course }}</p>
<p>Year: {{ student.year }}</p>
<hr>
<!-- Nested lookup -->
<h3>Nested Data:</h3>
<p>Name: {{ data.student.name }}</p>
<p>Age: {{ data.student.age }}</p>
</body>
</html>
โ๏ธ Step 4: Run Server
python manage.py runserver
๐ Step 5: Output
๐ http://127.0.0.1:8000/demo/
โ Output:
Name: Juhi Marks: First Mark: 78 Second Mark: 85 โข 78 โข 85 โข 90 Student Info: Course: MCA Year: 2nd Year Nested Data: Name: Aman Age: 22
๐ง Variable Lookup Explained ๐ฅ
โ 1. Dictionary Lookup
{{ student.course }}
๐ Looks for key "course"
โ 2. List Lookup
{{ marks.0 }}
๐ First element
โ 3. Nested Lookup
{{ data.student.name }}
๐ Step-by-step:
- data โ dictionary
- student โ inner dictionary
- name โ value
๐ง Lookup Order (Important Theory Question)
When Django sees:
{{ obj.key }}
It checks in order:
- Dictionary key
- Object attribute
- List index
โ ๏ธ Common Errors
โ Using brackets
โ Wrong:
{{ marks[0] }}
โ Correct:
{{ marks.0 }}
โ Wrong key
๐ Must match dictionary:
student = {"course": "MCA"}
โ Missing variable
๐ Ensure passed in context:
return render(..., {'name': name})
๐งช Practice Questions
- Access third element of list
- Create nested dictionary and display values
- Display all marks in table
- Combine dictionary + list
๐ค Viva Questions & Answers
1. What is context in Django?
Context is a dictionary used to pass multiple variables from view to template.
2. How many variables can be passed in context?
Any number of variables can be passed. There is no strict limit.
3. How does Django perform variable lookup?
It checks dictionary keys first, then object attributes, and finally list indices.
4. How to access list elements in template?
Using dot notation like marks.0, marks.1.
5. Can we use brackets in templates?
No, Django templates do not support bracket notation like Python.
6. What is nested lookup?
Accessing values inside nested structures like dictionary inside dictionary.
7. What happens if key is not found?
Django displays empty output instead of error.
8. Why is lookup order important?
It helps understand how Django resolves variables and avoids confusion.
9. Can we pass objects also?
Yes, model objects can also be passed and accessed similarly.
10. What is dynamic rendering?
Displaying data dynamically based on context variables.
๐ Navigation
๐ Next Post: Using {% include %} for Header and Footer
๐ Back to List: Django Programs (60 Questions with Solutions)
Further Reading
Introduction to Django Framework and its Features
Examples of Array Functions in PHP
Registration Form Using PDO in PHP
Inserting Information from Multiple CheckBox Selection in a Database Table in PHP
- Angular
- ASP.NET
- C
- C#
- C++
- CSS
- Dot Net Framework
- HTML
- IoT
- Java
- JavaScript
- Kotlin
- PHP
- Power Bi
- Python
- Scratch 3.0
- TypeScript
- VB.NET
