Django

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

You may also like...

Leave a Reply

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