Django

Display a List Using {% for %} Loop in Django Template

๐Ÿ“Œ 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 subjects is 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

  1. View creates a list
  2. Sends it to template
  3. Template loops through list
  4. 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

  1. Display list of 5 students
  2. Display list of numbers (1โ€“10)
  3. Display subjects in ordered list (<ol>)
  4. 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

Django Practice Exercise

Examples of Array Functions in PHP

Basic Programs in PHP

Registration Form Using PDO in PHP

Inserting Information from Multiple CheckBox Selection in a Database Table in PHP

programmingempire

princites.com

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *