Crispy Crispy - 1 year ago 120
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
print 'list is empty'

Django template for-else (my guess)

<h1>{{ game.title}}</h1>


{% for platform in game.platform_set.all %}
<td>{{ platform.system }} -- ${{ platform.price}}</td>
{% else %}
<td>No Platforms</td>
{% endfor %}


<a href="{% url 'video_games:profile' %}"></a>

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download