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
- View sends list of students
{% for %}loops through each student{% if %}checks attendance- 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
- Add grade based on attendance
- Highlight low attendance rows
- Count eligible students
- 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
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
