๐ 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
