Boolean Operators - Django Template Tags
Django provides the ability to use conditional logic like if, else, and elif using Boolean operators within templates. This allows you to conditionally render parts of a webpage based on values passed from your views.
Syntax:
{% if variable boolean_operator value %}
// statements
{% endif %}
We can also use multiple operators such as and, or, and not to build more complex conditions.
For Example:
{% 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 %}
Notice that 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.
Example of Boolean Operators in Django
Let’s look at a working example using a Django project geeksforgeeks with an app named geeks.
Refer to the following articles to check how to create a project and an app in Django.
After setting up the project and app folder, follow these steps:
1. views.py
Create a view through which we will pass the context dictionary in geeks/views.py:
from django.shortcuts import render
def geeks_view(request):
context = {
"data": 99,
}
return render(request, "geeks.html", context)
2. urls.py
Create a url path to map to this view in geeks/urls.py,
from django.urls import path
from .views import geeks_view
urlpatterns = [
path('', geeks_view),
]
3. templates/geeks.html
Create a geeks.html file in templates folder of the app (geeks) if the folder already exists, if not then create the templates folder and then the geeks.html file in it:
{% if data == 99 %}
Value in data is: {{ data }}
{% else %}
Data is empty
{% endif %}
Run the app using command- python manage.py runserver and visit http://127.0.0.1:8000/ to see the output.
Common Boolean Operators in Django Templates
== (Equal to)
Returns True if values on both side of the operator are equal.
{% if somevar == "x" %}
somevar is equal to "x"
{% endif %}
!= (Not equal to)
Returns True if the values are not equal.
{% if somevar != "x" %}
somevar is not equal to "x"
{% endif %}
< (Less than)
Checks if the left value is less than the right value.
{% if somevar < 100 %}
somevar is less than 100
{% endif %}
> (Greater than)
Checks if the left value is greater than the right value.
{% if somevar > 0 %}
somevar is greater than 0
{% endif %}
<= (Less than or equal to)
Checks if the left value is less than or equal to the right value.
{% if somevar <= 100 %}
somevar is less than or equal to 100
{% endif %}
>= (Greater than or equal to)
Checks if the left value is greater than or equal to the right value.
{% if somevar >= 1 %}
somevar is greater than or equal to 1
{% endif %}
in (Contained within)
Tests if a value exists in a list, string, or QuerySet.
{% if "bc" in "abcdef" %}
"bc" is found in "abcdef"
{% endif %}
{% if "hello" in greetings %}
"hello" is in the greetings list or set
{% endif %}
{% if user in users %}
user is present in the users QuerySet
{% endif %}
not in (Not contained within)
It's the negation (opposite) of the in operator.
{% if "z" not in "abc" %}
"z" is not found in "abc"
{% endif %}
is (Object identity)
Tests if two values are the same object.
{% if somevar is True %}
This appears if and only if somevar is True.
{% endif %}
{% if somevar is None %}
This appears if somevar is None, or if somevar is not found in the context.
{% endif %}
is not (Negated object identity)
Tests if two values are not the same object.
{% if somevar is not True %}
somevar is not exactly True
{% endif %}
{% if somevar is not None %}
somevar is not None
{% endif %}
and, or, not (Logical operators)
- and: All conditions must be True
- or: At least one condition must be True
- not: Reverses the boolean value
{% if user.is_active and user.is_staff %}
User is active and is a staff member
{% endif %}
{% if user.is_superuser or user.is_staff %}
User is either a superuser or staff
{% endif %}
{% if not user.is_authenticated %}
User is not logged in
{% endif %}