Display Marks List and Pass/Fail Result using {% for %} and {% if %}

πŸ“Œ 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

  1. View sends list of marks
  2. {% for %} loops through each mark
  3. {% if %} checks condition
  4. 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

  1. Display grade (A/B/C) instead of pass/fail
  2. Count total pass students
  3. Highlight highest marks
  4. 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: Display Product Details in Table Format using Template Loop
πŸ‘‰ 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 *