Display Product Details in Table Format using Template Loop

πŸ“Œ 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

  1. View sends list of products
  2. {% for %} loops through each product
  3. Table rows are generated dynamically
  4. 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

  1. Add total column (price Γ— qty)
  2. Add grand total
  3. Highlight expensive products
  4. 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

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

Leave a Reply

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