๐ Introduction
In Django, data is passed from view โ template using a context dictionary.
๐ Types of data we can pass:
- Variables
- Lists
- Dictionaries
This program demonstrates:
- Passing a list
- Passing a dictionary
- Displaying both in template
๐ฏ Program Statement
๐ Pass dictionary and list context data to a template and display values.
โ๏ธ Step 1: Create View
๐ File: views.py
๐น Path:
myproject/myapp/views.py
๐น Code:
from django.shortcuts import render
def student_data(request):
# List data
subjects = ["Python", "Java", "Django", "AI"]
# Dictionary data
student = {
"name": "Cherry",
"age": 18,
"course": "BCA"
}
return render(request, 'student.html', {
'subjects': subjects,
'student': student
})
๐ง Explanation
subjectsโ liststudentโ dictionary- Both passed using context
โ๏ธ 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('student/', views.student_data),
]
โ๏ธ Step 3: Create Template
๐ File: student.html
๐น Path:
myproject/templates/student.html
๐น Code:
<!DOCTYPE html>
<html>
<head>
<title>Student Data</title>
</head>
<body>
<h1>Student Details</h1>
<!-- Dictionary Access -->
<p>Name: {{ student.name }}</p>
<p>Age: {{ student.age }}</p>
<p>Course: {{ student.course }}</p>
<hr>
<h2>Subjects List</h2>
<!-- List Access -->
<ul>
{% for sub in subjects %}
<li>{{ sub }}</li>
{% endfor %}
</ul>
</body>
</html>
โ๏ธ Step 4: Run Server
python manage.py runserver
๐ Step 5: Output
๐ http://127.0.0.1:8000/student/
โ Output:
Student Details Name: Cherry Age: 18 Course: BCA Subjects List: โข Python โข Java โข Django โข AI
๐ง How It Works
- View creates list and dictionary
- Passes them via context
- Template accesses:
- Dictionary โ
student.name - List โ loop using
{% for %}
- Dictionary โ
- Data displayed
๐ง Syntax Summary
โ Passing Data:
return render(request, 'file.html', {'key': value})
โ Access Dictionary:
{{ student.name }}
โ Loop List:
{% for item in list %}
{{ item }}
{% endfor %}
โ ๏ธ Common Errors
โ Key mismatch
๐ Must match:
'student': student
{{ student.name }}
โ Loop not working
๐ Check:
{% for sub in subjects %}
โ Dictionary not displaying
๐ Use dot notation:
{{ student.name }}
NOT:
{{ student['name'] }}
๐งช Practice Questions
- Pass list of 5 students
- Pass dictionary of employee
- Display list in table format
- Add nested dictionary
๐ค Viva Questions & Answers
1. What is context in Django?
Context is a dictionary used to pass data from view to template. It contains key-value pairs.
2. How do we pass data to template?
Using render(request, template, context). Context contains variables to display.
3. How do we access dictionary in template?
Using dot notation like {{ student.name }}. It is simple and readable.
4. How do we display list data?
Using {% for %} loop to iterate and display each item.
5. Can we pass multiple data types?
Yes, we can pass lists, dictionaries, strings, numbers, and objects.
6. What is key-value pair in context?
Key is variable name used in template. Value is actual data from view.
7. Why not use student['name']?
Django templates use dot notation for simplicity and security.
8. What is dynamic data rendering?
Displaying data that comes from backend instead of static HTML.
9. What happens if key is wrong?
Template will not display data and may show empty output.
10. Can we pass database data similarly?
Yes, QuerySets can also be passed like lists and displayed in templates.
๐ Navigation
๐ Next Post: Multiple Context and Variable Lookup
๐ 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
