๐ 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
marksis 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
- View sends
marks - Template checks condition
- 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
- Display grade (A/B/C)
- Check voting eligibility (age โฅ 18)
- Check even/odd number
- 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
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
