Reputation: 3716
In the Django template language is there away to use the the else clause with a for loop? I relies I can use an if check before the for loop but that gets repetitive.
python for-else
list = []
for i in list:
print i
else:
print 'list is empty'
Django template for-else (my guess)
<h1>{{ game.title}}</h1>
<table>
<tr>
{% for platform in game.platform_set.all %}
<td>{{ platform.system }} -- ${{ platform.price}}</td>
{% else %}
<td>No Platforms</td>
{% endfor %}
</tr>
</table>
<a href="{% url 'video_games:profile' game.id %}"></a>
Upvotes: 20
Views: 11228
Reputation: 247
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
Upvotes: -1
Reputation: 411340
Use for...empty
, which is basically the Django equivalent (replaces the else
keyword with empty
).
Upvotes: 33