Display Attendance Report with Conditional Status inside a Loop

πŸ“Œ Introduction

In real applications like:

  • Student portals
  • LMS systems

We need to display:

  • Attendance %
  • Status (Present / Short Attendance)

πŸ‘‰ This is done using:

  • {% for %} β†’ loop
  • {% if %} β†’ condition

🎯 Program Statement

πŸ‘‰ Display attendance report with conditional status inside a loop.


🧠 Problem Logic

πŸ‘‰ Condition:

  • If attendance β‰₯ 75 β†’ Eligible
  • Else β†’ Short Attendance

βš™οΈ Step 1: Create View


πŸ“ File: views.py

πŸ”Ή Path:

myproject/myapp/views.py

πŸ”Ή Code:

from django.shortcuts import render

def attendance_report(request):

students = [
{"name": "Aman", "attendance": 82},
{"name": "Riya", "attendance": 68},
{"name": "Karan", "attendance": 75},
{"name": "Neha", "attendance": 60},
]

return render(request, 'attendance.html', {
'students': students
})

🧠 Explanation

  • List of students with attendance %
  • Passed as context
  • Template will apply condition

βš™οΈ 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('attendance/', views.attendance_report),
]

βš™οΈ Step 3: Create Template


πŸ“ File: attendance.html

πŸ”Ή Path:

myproject/templates/attendance.html

πŸ”Ή Code:

<!DOCTYPE html>
<html>
<head>
<title>Attendance Report</title>
</head>
<body>

<h1>Attendance Report</h1>

<table border="1" cellpadding="10">
<tr>
<th>S.No</th>
<th>Name</th>
<th>Attendance (%)</th>
<th>Status</th>
</tr>

{% for s in students %}
<tr>
<td>{{ forloop.counter }}</td>
<td>{{ s.name }}</td>
<td>{{ s.attendance }}</td>

<td>
{% if s.attendance >= 75 %}
<span style="color:green;">Eligible</span>
{% else %}
<span style="color:red;">Short Attendance</span>
{% endif %}
</td>

</tr>
{% endfor %}

</table>

</body>
</html>

βš™οΈ Step 4: Run Server

python manage.py runserver

🌐 Step 5: Output

πŸ‘‰ http://127.0.0.1:8000/attendance/


βœ… Output:

S.No   Name   Attendance   Status
1 Aman 82 Eligible
2 Riya 68 Short Attendance
3 Karan 75 Eligible
4 Neha 60 Short Attendance

🧠 How It Works

  1. View sends list of students
  2. {% for %} loops through each student
  3. {% if %} checks attendance
  4. Status displayed dynamically

πŸ”₯ Key Concepts


Loop:

{% for s in students %}

Condition:

{% if s.attendance >= 75 %}

Combined Logic:

{% for s in students %}
{% if s.attendance >= 75 %}
Eligible
{% else %}
Short Attendance
{% endif %}
{% endfor %}

⚠️ Common Errors


❌ Wrong key

πŸ‘‰ Must match:

"attendance"

❌ Condition not working

πŸ‘‰ Check:

{% if s.attendance >= 75 %}

❌ Loop not closed

πŸ‘‰ Use:

{% endfor %}

πŸ§ͺ Practice Questions

  1. Add grade based on attendance
  2. Highlight low attendance rows
  3. Count eligible students
  4. Display in Bootstrap table

🎀 Viva Questions & Answers


1. What is {% for %} used for?

It is used to iterate over a list and display each element.


2. What is {% if %} used for?

It is used to apply conditional logic in templates.


3. Can we combine {% for %} and {% if %}?

Yes, they are often used together to apply logic on each item.


4. What is real-world use of this program?

Attendance systems, result analysis, dashboards.


5. What is dynamic condition rendering?

Displaying output based on data values at runtime.


6. What is forloop.counter?

It gives serial number starting from 1.


7. What happens if attendance is exactly 75?

Condition is true, so student is eligible.


8. Can we use multiple conditions?

Yes, using {% elif %}.


9. Where should logic be written?

Simple logic β†’ template
Complex logic β†’ view


10. Why is this program important?

It demonstrates real-world conditional processing in templates.


πŸ‘‰ Next Post: Template Filters (upper, lower, length, date)
πŸ‘‰ 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 *