๐ Introduction
In Django, we often need to display multiple items like:
- subjects
- products
- students
This is done using the {% for %} loop in templates.
๐ฏ Program Statement
๐ Display a list of subjects using {% for %} loop in a template.
โ๏ธ Step 1: Create Project and App
django-admin startproject myproject
cd myproject
python manage.py startapp myapp
โ๏ธ Step 2: Register App
๐ File: settings.py
๐น Path:
myproject/myproject/settings.py
๐น Code:
INSTALLED_APPS = [
...
'myapp',
]
โ๏ธ Step 3: Configure Templates
๐ File: settings.py
๐น Path:
myproject/myproject/settings.py
๐น Code:
import os
TEMPLATES = [
{
'DIRS': [os.path.join(BASE_DIR, 'templates')],
'APP_DIRS': True,
},
]
โ๏ธ Step 4: Create View
๐ File: views.py
๐น Path:
myproject/myapp/views.py
๐น Code:
from django.shortcuts import render
def subject_list(request):
subjects = ["Python", "Java", "Django", "Data Science", "AI"]
return render(request, 'subjects.html', {'subjects': subjects})
๐ง Explanation
- A list named
subjectsis created - It is passed to template using dictionary
- Template will loop through this list
โ๏ธ Step 5: 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('subjects/', views.subject_list),
]
โ๏ธ Step 6: Create Template
๐ File: subjects.html
๐น Path:
myproject/templates/subjects.html
๐น Code:
<!DOCTYPE html>
<html>
<head>
<title>Subjects</title>
</head>
<body>
<h1>Subject List</h1>
<ul>
{% for sub in subjects %}
<li>{{ sub }}</li>
{% endfor %}
</ul>
</body>
</html>
๐ง Explanation of Loop
{% for sub in subjects %}
๐ Loop starts
{{ sub }}
๐ Displays each item
{% endfor %}
๐ Loop ends
โ๏ธ Step 7: Run Server
python manage.py runserver
๐ Step 8: Output
Open:
๐ http://127.0.0.1:8000/subjects/
โ Output:
Subject List
โข Python
โข Java
โข Django
โข Data Science
โข AI
๐ง How It Works
- View creates a list
- Sends it to template
- Template loops through list
- Displays each item
โ ๏ธ Common Errors
โ List not displaying
๐ Check:
{'subjects': subjects}
โ Template syntax error
๐ Always write:
{% for item in list %}
โ Missing {% endfor %}
๐ Loop must always be closed
๐งช Practice Questions
- Display list of 5 students
- Display list of numbers (1โ10)
- Display subjects in ordered list (
<ol>) - Display list in table format
๐ค Viva Questions & Answers
1. What is {% for %} loop in Django?
It is used to iterate over a list or collection in a template. It helps display multiple items dynamically.
2. Where is the list created?
The list is created in the view and passed to the template using a dictionary.
3. What is the role of {% endfor %}?
It marks the end of the loop block. Without it, Django will throw an error.
4. What is loop variable?
It is a temporary variable used inside loop to access each element. Example: sub in {% for sub in subjects %}.
5. Can we use loops in HTML directly?
No, HTML does not support loops. Django template language provides looping functionality.
6. What type of data can be used in loop?
Lists, tuples, querysets, and dictionaries can be used in loops.
7. What happens if list is empty?
Nothing will be displayed unless {% empty %} block is used.
8. What is dynamic rendering?
It means displaying data that changes based on input or logic. Loop rendering is dynamic.
9. Can we nest loops?
Yes, loops can be nested inside each other for complex data structures.
10. What is template tag?
Template tags are special syntax like {% for %}, {% if %} used for logic inside templates.
๐ Navigation
๐ Next Post: GET Form โ Accept User Input and Display Greeting
๐ 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
