A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python/if-django-template-tags/ below:

if - Django Template Tags

if - Django Template Tags

Last Updated : 12 Jul, 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 %}

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:

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:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4