for … empty loop – 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 paassing data from view to template, but also provides some limited features of a programming such as variables, for loops, comments, extends etc.
This article revolves about how to use for tag with empty in Templates. for tag loops over each item in an array, making the item available in a context variable. The for tag can take an optional {% empty %} clause whose text is displayed if the given array is empty or could not be found. This is basically used as a condition to be followed to check if queryset is empty and what action to be performed in the same scenario.

Syntax
{% for i in list %}
// Do this in non - empty condition
{% empty %}
// Do this in empty condition
{% endfor %}
Example

For example, to display a list of athletes provided in athlete_list:

filter_none

edit
close

play_arrow

link
brightness_4
code

<ul>
{% for athlete in athlete_list %}
    <li>{{ athlete.name }}</li>
{% empty %}
    <li>Sorry, no athletes in this list.</li>
{% endfor %}
</ul>

chevron_right


The above is equivalent to – but shorter, cleaner, and possibly faster than – the following:

filter_none

edit
close

play_arrow

link
brightness_4
code

<ul>
  {% if athlete_list %}
    {% for athlete in athlete_list %}
      <li>{{ athlete.name }}</li>
    {% endfor %}
  {% else %}
    <li>Sorry, no athletes in this list.</li>
  {% endif %}
</ul>

chevron_right


for … empty – Django template Tags Explanation

Illustration of How to use for …empty 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,

filter_none

edit
close

play_arrow

link
brightness_4
code

# import Http Response from django
from django.shortcuts import render
  
# create a function
def geeks_view(request):
    # create a dictionary
    context = {
        "data" : [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
    }
    # return response
    return render(request, "geeks.html", context)

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Create a template in templates/geeks.html,

filter_none

edit
close

play_arrow

link
brightness_4
code

{% for i in data %}
    <div class="row">
        {{ i }}
    </div>
{% endfor %}

chevron_right


Let’s check what is displayed on “/” are displayed in the template.
cycle-django-template-tags
Anything enclosed between for tag would be repeated, the number of times the loop is run.

Now let’s pass an empty array and use empty tag along with for tag.
In geeks/views.py,

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Now in templates/geeks.html,

filter_none

edit
close

play_arrow

link
brightness_4
code

{% for i in data %}
    <div class="row">
        {{ i }}
    </div>
    {% empty %}
    <h4>There is nothing in this list</h4>
{% endfor %}

chevron_right


Now, check http://127.0.0.1:8000/,
for-empty-django-template-tags

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Software Developer at GeeksForGeeks

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.