Boolean Operators - Django Template Tags
Last Updated :
15 May, 2025
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:
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 %}
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.
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:
Python
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,
Python
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:
html
{% 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.
HTML
{% 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.
HTML
{% if "z" not in "abc" %}
"z" is not found in "abc"
{% endif %}
is (Object identity)
Tests if two values are the same object.
HTML
{% 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.
HTML
{% 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
HTML
{% 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 %}
Similar Reads
Django Templates Templates are the third and most important part of Django's MVT Structure. A Django template is basically an HTML file that can also include CSS and JavaScript. The Django framework uses these templates to dynamically generate web pages that users interact with. Since Django primarily handles the ba
7 min read
variables - Django Templates Prerequisite- Django TemplatesIn Django, templates are used to dynamically generate HTML content by combining static HTML with dynamic data from views. One of the simplest and most useful features of Django templates is the use of variables. Variables allow you to display data passed from a view ins
2 min read
Django Template Tags Prerequisite: What are Django Templates?Django provides a powerful templating engine that allows us to add logic directly into our templates using template tags. These tags enable everything from control structures (like if and for loops), to inserting dynamic content, to template inheritance. Templ
4 min read
extends - Django Template Tags Djangoâs {% extends %} tag allows you to reuse a base template across multiple pages, so you donât have to repeat the same HTML code in every template. This makes your templates cleaner, easier to maintain, and helps keep a consistent layout throughout your site.Syntax: {% extends 'base_template.htm
2 min read
if - Django Template Tags 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:
3 min read
for loop - Django Template Tags Django templates allow you to render dynamic data by embedding Python-like logic into HTML. The for loop is one of the most commonly used template tags, enabling you to iterate over lists or other iterable objects. It helps you display repeated content (like lists, tables, etc.) in your templates wi
3 min read
comment - 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 the template but also provide som
2 min read
include - 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
2 min read
url - Django Template Tag 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
3 min read
cycle - 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
3 min read