if - Django Template Tags
The {% if %} tag in Django templates allows us to control what content is displayed based on certain conditions. We can use it to show or hide parts of a page depending on whether a condition is met.
Syntax of the {% if %} Tag
{% if variable %}
// statements
{% else %}
// statements
{% endif %}
- condition: This is any expression that evaluates to True or False. It could be a variable, a comparison, or a function call.
Example:
{% if athlete_list %}
Number of athletes: {{ athlete_list|length }}
{% elif athlete_in_locker_room_list %}
Athletes should be out of the locker room soon!
{% else %}
No athletes.
{% endif %}
Explanation:
- {% if athlete_list %}: If the athlete_list is not empty, the number of athletes is displayed using the {{ athlete_list|length }} filter.
- {% elif athlete_in_locker_room_list %}: If athlete_list is empty but athlete_in_locker_room_list has a value, a message saying athletes should be out of the locker room soon is displayed.
- {% else %}: If neither condition is true, the message "No athletes." is shown.
Using {% if %} in a Django Application
Illustration of How to use if tag in Django templates using an example, consider a project named "geeksforgeeks" having an app named "geeks".
Refer to the following articles to check how to create a project and an app in Django.
Let’s go step-by-step to see how to use the {% if %} tag in a Django app.
1. Creating the View
In the geeks/views.py file, we will create a view that passes data to the template.
from django.shortcuts import render
def geeks_view(request):
context = {
"data" : 99,
}
return render(request, "geeks.html", context)
Here, we're passing a key-value pair, where "data" has a value of 99, to the template.
2. Creating the URL Path
In the geeks/urls.py file, we map the URL to this view.
from django.urls import path
from .views import geeks_view
urlpatterns = [
path('', geeks_view),
]
3. Creating the Template
Now, create a template geeks.html in the templates directory where we’ll use the {% if %} tag.
{% if data %}
Value in data is : - {{ data }}
{% else %}
Data is empty
{% endif%}
4. Viewing the Result
Now, visit http://127.0.0.1:8000/ in your browser. You should see:
Handling Empty Values
If you pass an empty value, like False, the {% else %} block will be executed.
from django.shortcuts import render
def geeks_view(request):
context = {
"data" : False,
}
return render(request, "geeks.html", context)
Now, check "http://127.0.0.1:8000/",
Advanced Usage of the {% if %} Tag
You can enhance the functionality of {% if %} tags by using logical operators like and, or, and not to combine multiple conditions.
Using and, or, and not Operators
{% if athlete_list and coach_list %}
Both athletes and coaches are available.
{% endif %}
{% if not athlete_list %}
There are no athletes.
{% endif %}
{% if athlete_list or coach_list %}
There are some athletes or some coaches.
{% endif %}
{% if not athlete_list or coach_list %}
There are no athletes or there are some coaches.
{% endif %}
{% if athlete_list and not coach_list %}
There are some athletes and absolutely no coaches.
{% endif %}
Example context:
athlete_list = ['Alice', 'Bob']
coach_list = ['Coach Mike']
Output:
Both athletes and coaches are available.
There are some athletes or some coaches.
There are no athletes or there are some coaches.
Explanation:
- and: Checks if both conditions are true.
- or: Checks if at least one of the conditions is true.
- not: Negates the condition.