Open In App

if - Django Template Tags

Last Updated : 17 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

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.

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:

if-django-template-tags

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/", 

empty-tag-Django-template-tags

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.

Next Article
Practice Tags :

Similar Reads