Crispy Crispy - 12 days ago 5
Python Question

Django Template Language: Using a for loop with else

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>

Answer

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

Comments