Friday, November 15, 2024
Google search engine
HomeLanguagesif – Django Template Tags

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 neveropen having an app named Lazyroar. 

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 Lazyroar/views.py,

Python3




# import Http Response from django
from django.shortcuts import render
  
# create a function
def Lazyroar_view(request):
    # create a dictionary
    context = {
        "data" : 99,
    }
    # return response
    return render(request, "Lazyroar.html", context)


Create a url path to map to this view. In Lazyroar/urls.py,

Python3




from django.urls import path
 
# importing views from views.py
from .views import Lazyroar_view
 
urlpatterns = [
    path('', Lazyroar_view),
]


Create a template in templates/Lazyroar.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. 

if-django-template-tags

{% 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 Lazyroar/views.py, 

Python3




## import Http Response from django
from django.shortcuts import render
  
# create a function
def Lazyroar_view(request):
    # create a dictionary
    context = {
        "data" : False,
    }
    # return response
    return render(request, "Lazyroar.html", context)


Now, check http://127.0.0.1:8000/

empty-tag-Django-template-tags

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 %}

RELATED ARTICLES

Most Popular

Recent Comments