Django

Display Eligibility Status using {% if %} Condition in Django Templates

๐Ÿ“Œ Introduction

In Django templates, we can apply conditions using:

๐Ÿ‘‰ {% if %}

This allows us to:

  • Check conditions
  • Display output dynamically
  • Implement decision-making logic

๐ŸŽฏ Program Statement

๐Ÿ‘‰ Display eligibility status using {% if %} condition in templates.


๐Ÿง  Problem Logic

๐Ÿ‘‰ Condition:

  • If marks โ‰ฅ 40 โ†’ Pass / Eligible
  • Else โ†’ Fail / Not Eligible

โš™๏ธ Step 1: Create View


๐Ÿ“ File: views.py

๐Ÿ”น Path:

myproject/myapp/views.py

๐Ÿ”น Code:

from django.shortcuts import render

def eligibility(request):

marks = 55 # Change value to test

return render(request, 'eligibility.html', {
'marks': marks
})

๐Ÿง  Explanation

  • marks is passed from view
  • Template will check 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('eligibility/', views.eligibility),
]

โš™๏ธ Step 3: Create Template


๐Ÿ“ File: eligibility.html

๐Ÿ”น Path:

myproject/templates/eligibility.html

๐Ÿ”น Code:

<!DOCTYPE html>
<html>
<head>
<title>Eligibility Check</title>
</head>
<body>

<h1>Eligibility Status</h1>

<p>Marks: {{ marks }}</p>

{% if marks >= 40 %}
<h2 style="color:green;">Eligible (Pass)</h2>
{% else %}
<h2 style="color:red;">Not Eligible (Fail)</h2>
{% endif %}

</body>
</html>

โš™๏ธ Step 4: Run Server

python manage.py runserver

๐ŸŒ Step 5: Output

๐Ÿ‘‰ http://127.0.0.1:8000/eligibility/


โœ… Output (marks = 55):

Marks: 55
Eligible (Pass)

โŒ Output (marks = 30):

Marks: 30
Not Eligible (Fail)

๐Ÿง  How It Works

  1. View sends marks
  2. Template checks condition
  3. Displays result based on condition

๐Ÿ”ฅ Syntax of {% if %}


Basic:

{% if condition %}
...
{% endif %}

With else:

{% if condition %}
...
{% else %}
...
{% endif %}

With elif:

{% if marks >= 75 %}
Distinction
{% elif marks >= 50 %}
Pass
{% else %}
Fail
{% endif %}

โš ๏ธ Common Errors


โŒ Missing {% endif %}

๐Ÿ‘‰ Always close block


โŒ Wrong syntax

โŒ Wrong:

{{ if marks >= 40 }}

โœ… Correct:

{% if marks >= 40 %}

โŒ Variable not passed

๐Ÿ‘‰ Ensure:

{'marks': marks}

๐Ÿงช Practice Questions

  1. Display grade (A/B/C)
  2. Check voting eligibility (age โ‰ฅ 18)
  3. Check even/odd number
  4. Show message if list is empty

๐ŸŽค Viva Questions & Answers


1. What is {% if %} in Django?

It is used to apply conditional logic in templates. It allows decision-making during rendering.


2. What is the difference between {% if %} and Python if?

Both are similar in logic, but Django uses template syntax for rendering.


3. Can we use comparison operators?

Yes, operators like >=, <=, ==, != can be used in templates.


4. What is {% else %}?

It defines alternative block when condition is false.


5. What is {% elif %}?

It allows checking multiple conditions in sequence.


6. What happens if variable is missing?

Condition fails silently and may produce incorrect output.


7. Can we nest if statements?

Yes, nested conditions are allowed.


8. What is dynamic decision-making?

Displaying output based on data conditions at runtime.


9. Where should logic be written: view or template?

Complex logic โ†’ view
Simple conditions โ†’ template


10. Why is {% if %} important?

It enables dynamic rendering and user-specific output.


๐Ÿ”— Navigation

๐Ÿ‘‰ Next Post: Use {% for %} + {% if %} for Pass/Fail List
๐Ÿ‘‰ 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 *