if - Django Template Tags
Last Updated :
17 May, 2025
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:
html
{% 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.
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.
Python
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.
Python
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.
html
{% 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.
Python
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
HTML
{% 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.
Similar Reads
Django Templates
Templates are the third and most important part of Django's MVT Structure. A Django template is basically an HTML file that can also include CSS and JavaScript. The Django framework uses these templates to dynamically generate web pages that users interact with. Since Django primarily handles the ba
7 min read
variables - Django Templates
Prerequisite- Django TemplatesIn Django, templates are used to dynamically generate HTML content by combining static HTML with dynamic data from views. One of the simplest and most useful features of Django templates is the use of variables. Variables allow you to display data passed from a view ins
2 min read
Django Template Tags
Prerequisite: What are Django Templates?Django provides a powerful templating engine that allows us to add logic directly into our templates using template tags. These tags enable everything from control structures (like if and for loops), to inserting dynamic content, to template inheritance. Templ
4 min read
extends - Django Template Tags
A Django template is a text document or a Python string marked-up using the Django template language. Django being a powerful Batteries included framework provides convenience to rendering data in a template. Django templates not only allow passing data from view to template, but also provides some
2 min read
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:
3 min read
for loop - Django Template Tags
Django templates allow you to render dynamic data by embedding Python-like logic into HTML. The for loop is one of the most commonly used template tags, enabling you to iterate over lists or other iterable objects. It helps you display repeated content (like lists, tables, etc.) in your templates wi
3 min read
comment - Django template tags
A Django template is a text document or a Python string marked-up using the Django template language. Django being a powerful Batteries included framework provides convenience to rendering data in a template. Django templates not only allow passing data from view to the template but also provide som
2 min read
include - Django Template Tags
A Django template is a text document or a Python string marked-up using the Django template language. Django being a powerful Batteries included framework provides convenience to rendering data in a template. Django templates not only allow passing data from view to template, but also provides some
2 min read
url - Django Template Tag
A Django template is a text document or a Python string marked-up using the Django template language. Django being a powerful Batteries included framework provides convenience to rendering data in a template. Django templates not only allow passing data from view to template, but also provides some
3 min read
cycle - Django Template Tags
A Django template is a text document or a Python string marked-up using the Django template language. Django being a powerful Batteries included framework provides convenience to rendering data in a template. Django templates not only allow passing data from view to template, but also provides some
3 min read