๐ Introduction
In Django templates, we can display structured data like:
- Product lists
- Student records
- Reports
๐ using:
{% for %}loop- HTML tables
๐ฏ Program Statement
๐ Display product details in table format using template loop.
๐ง Problem Logic
We will:
- Create product data (list of dictionaries)
- Loop through it
- Display in table
โ๏ธ Step 1: Create View
๐ File: views.py
๐น Path:
myproject/myapp/views.py
๐น Code:
from django.shortcuts import render
def product_list(request):
products = [
{"name": "Laptop", "price": 50000, "qty": 2},
{"name": "Mobile", "price": 20000, "qty": 5},
{"name": "Tablet", "price": 15000, "qty": 3},
]
return render(request, 'product.html', {
'products': products
})
๐ง Explanation
- Each product is a dictionary
- All products stored in a list
- Passed as 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('products/', views.product_list),
]
โ๏ธ Step 3: Create Template
๐ File: product.html
๐น Path:
myproject/templates/product.html
๐น Code:
<!DOCTYPE html>
<html>
<head>
<title>Product Table</title>
</head>
<body>
<h1>Product Details</h1>
<table border="1" cellpadding="10">
<tr>
<th>S.No</th>
<th>Product Name</th>
<th>Price</th>
<th>Quantity</th>
</tr>
{% for p in products %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ p.name }}</td>
<td>{{ p.price }}</td>
<td>{{ p.qty }}</td>
</tr>
{% endfor %}
</table>
</body>
</html>
๐ง Special Feature ๐ฅ
forloop.counter
{{ forloop.counter }}
๐ Automatically generates serial number
โ๏ธ Step 4: Run Server
python manage.py runserver
๐ Step 5: Output
๐ http://127.0.0.1:8000/products/
โ Output:
S.No Product Price Quantity
1 Laptop 50000 2
2 Mobile 20000 5
3 Tablet 15000 3
๐ง How It Works
- View sends list of products
{% for %}loops through each product- Table rows are generated dynamically
- Data displayed in structured format
๐ฅ Key Concepts
Loop:
{% for p in products %}
Access dictionary:
{{ p.name }}
{{ p.price }}
Serial number:
{{ forloop.counter }}
โ ๏ธ Common Errors
โ Wrong key name
๐ Must match:
"name"
{{ p.name }}
โ Loop not closing
๐ Always:
{% endfor %}
โ Empty table
๐ Ensure data is passed:
{'products': products}
๐งช Practice Questions
- Add total column (price ร qty)
- Add grand total
- Highlight expensive products
- Display table with Bootstrap
๐ค Viva Questions & Answers
1. What is {% for %} loop in Django?
It is used to iterate over a list or collection and display each element.
2. What is forloop.counter?
It gives the current iteration number starting from 1. It is used for serial numbers.
3. How do we access dictionary values in template?
Using dot notation like {{ p.name }}.
4. What is structured data display?
Displaying data in organized format like tables.
5. Can we use nested loops?
Yes, Django supports nested loops.
6. What happens if list is empty?
Table rows will not be generated.
7. Why use table format?
To display data in rows and columns for better readability.
8. Can we calculate values in template?
Basic operations are possible but complex logic should be in view.
9. What is dynamic table?
A table generated from data instead of static HTML.
10. Where is this used in real life?
Student results, attendance, product listings, admin dashboards.
๐ Navigation
๐ Next Post: Attendance Report with Conditional Status
๐ 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
