๐ Introduction
In Django templates, we can combine:
{% for %}โ to loop through list{% if %}โ to apply condition
๐ This allows us to:
- Process multiple values
- Apply logic to each value
๐ฏ Program Statement
๐ Display marks list and pass/fail result using {% for %} and {% if %}.
๐ง Problem Logic
For each mark:
- If marks โฅ 40 โ Pass
- Else โ Fail
โ๏ธ Step 1: Create View
๐ File: views.py
๐น Path:
myproject/myapp/views.py
๐น Code:
from django.shortcuts import render
def marks_result(request):
marks = [78, 35, 90, 42, 25]
return render(request, 'marks_result.html', {
'marks': marks
})
๐ง Explanation
- List of marks is created
- Passed to template
- Logic will be applied in template
โ๏ธ 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('result/', views.marks_result),
]
โ๏ธ Step 3: Create Template
๐ File: marks_result.html
๐น Path:
myproject/templates/marks_result.html
๐น Code:
<!DOCTYPE html>
<html>
<head>
<title>Marks Result</title>
</head>
<body>
<h1>Marks and Result</h1>
<table border="1" cellpadding="10">
<tr>
<th>Marks</th>
<th>Result</th>
</tr>
{% for m in marks %}
<tr>
<td>{{ m }}</td>
<td>
{% if m >= 40 %}
<span style="color:green;">Pass</span>
{% else %}
<span style="color:red;">Fail</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/result/
โ Output:
Marks Result
78 Pass
35 Fail
90 Pass
42 Pass
25 Fail
๐ง How It Works
- View sends list of marks
{% for %}loops through each mark{% if %}checks condition- Result displayed for each value
๐ฅ Key Concept
Loop:
{% for m in marks %}
Condition:
{% if m >= 40 %}
Combined Logic:
{% for m in marks %}
{% if m >= 40 %}
Pass
{% else %}
Fail
{% endif %}
{% endfor %}
โ ๏ธ Common Errors
โ Missing {% endfor %}
๐ Always close loop
โ Missing {% endif %}
๐ Always close condition
โ Wrong syntax
โ Wrong:
{{ if m >= 40 }}
โ Correct:
{% if m >= 40 %}
๐งช Practice Questions
- Display grade (A/B/C) instead of pass/fail
- Count total pass students
- Highlight highest marks
- Display marks in ordered list
๐ค Viva Questions & Answers
1. What is {% for %} in Django?
It is used to iterate over a list or collection. It helps display multiple items dynamically.
2. What is {% if %} in Django?
It is used for conditional logic in templates. It controls output based on conditions.
3. Can we use {% for %} and {% if %} together?
Yes, they are often used together to apply logic on each item in a list.
4. What is loop variable?
It is a temporary variable representing each element in loop. Example: m.
5. What happens if list is empty?
Nothing will be displayed unless {% empty %} is used.
6. Where should logic be written: view or template?
Simple conditions โ template
Complex logic โ view
7. What is dynamic rendering?
Displaying data based on conditions and runtime values.
8. Can we nest conditions inside loops?
Yes, conditions can be used inside loops for detailed logic.
9. What is benefit of combining loop and condition?
It allows processing each item individually and displaying customized output.
10. Why is this program important?
It demonstrates real-world logic like result processing, filtering, and conditional display.
๐ Navigation
๐ Next Post: Form Handling using GET Method
๐ 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
