Django

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: Form Handling using GET Method
๐Ÿ‘‰ 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 *