if – 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 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.
if – Django template Tags 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.
Now create a view through which we will pass the context dictionary,
In geeks/views.py,
Python3
from django.shortcuts import render
def geeks_view(request):
context = {
"data" : 99 ,
}
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
from .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.
{% else %}
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
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
if tags may use and, 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 %}
Last Updated :
13 Dec, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...