if – Django Template Tags

02 Dec, 2021
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 limited features of a programming such as variables, for loops, comments, extends, if else etc. This article revolves about how to use if tag in Templates. The {% if %} tag evaluates a variable, and if that variable is “true” (i.e. exists, is not empty, and is not a false boolean value) the contents of the block are output. 
Syntax:
{% 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 %}
In the above, if athlete_list is not empty, the number of athletes will be displayed by the {{ athlete_list|length }} variable.As one can see, the if tag may take one or several {% elif %} clauses, as well as an {% else %} clause that will be displayed if all previous conditions fail. These clauses are optional. 
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.  
How to Create a Basic Project using MVT in Django?
How to Create an App in Django ?
Now create a view through which we will pass the context dictionary, In geeks/views.py,
Python3
# import Http Response from djangofrom django.shortcuts import render  # create a functiondef geeks_view(request):    # create a dictionary    context = {        "data" : 99,    }    # return response    return render(request, "geeks.html", context)
Create a url path to map to this view. In geeks/urls.py,
Python3
from django.urls import path # importing views from views.pyfrom .views import geeks_view urlpatterns = [    path('', geeks_view),]
Create a template in templates/geeks.html,
html
{% if data %}Value in data is : - {{ data }}{% else %}Data is empty{% endif%}
Let’s check what is displayed on “/” are displayed in the template. 
Let’s check if {% else %} statement is working or not. Now let’s pass an empty array and use empty tag along with for tag. In geeks/views.py, 
Python3
## import Http Response from djangofrom django.shortcuts import render  # create a functiondef geeks_view(request):    # create a dictionary    context = {        "data" : False,    }    # return response    return render(request, "geeks.html", context)
Now, check http://127.0.0.1:8000/, 
if tags may use and, or or not to test a number of variables or to negate a given variable: 
{% 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 %}
surindertarika1234
Django-templates
Python Django
Python
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Iterate over a list in Python
How to iterate through Excel rows in Python?
Enumerate() in Python
Python Dictionary
Python OOPs Concepts
Different ways to create Pandas Dataframe
*args and **kwargs in Python
Python Classes and Objects
Introduction To PYTHON
Stack in Python