[Django]-Django Template Language: Using a for loop with else

33👍

Use for...empty, which is basically the Django equivalent (replaces the else keyword with empty).

👤mipadi

0👍

I know it’s a very old post. Adding an answer for future reference. There is no explicit way to achieve for..else but we can do something like the following.

{% for x in some_list %}
    ... awesome html and more here
    
    {% if forloop.last %}
        ... executes only if this is the last time through the loop
    {% endif %}

{% endfor %}

Hope this helps. More reading here

👤Ashwin

Leave a comment