Reputation: 19
{% for match in response %}
<tr>
<td>{{ match.i.position }}</td>
<td>{{ match.i.team.name }}</td>
<td>{{ match.i.games.played }}</td>
<td>{{ match.i.games.win.total }}</td>
<td>{{ match.i.games.lose.total }}</td>
<td>{{ match.i.games.win.percentage }}</td>
</tr>
</tr>
{% endfor %}
I am trying to increment the value of 'i' within a for loop in my standings.html django project. I am having trouble doing this as django doesnt seem to like me trying to increment values. Is there any way to fix this? I would like the value to start at 0 and increase by 1 in each iteration of the loop.
Upvotes: 0
Views: 451
Reputation: 8222
A general answer: you can't. By design . The template language is designed for displaying. Business logic should be written in Python. (Or, use Jinja instead of Django template language).
However, what you are asking is simply to access the loop iteration counter, and this is provided as part of {% for %}
functionality. Inside the loop, use {{ forloop.counter0 }}.
Upvotes: 0
Reputation: 8837
Well you should use {{forloop.counter0}}
so:
{% for match in response %}
<tr>
<td>{{ forloop.counter0 }}</td>
<td>{{ match.i.position }}</td>
<td>{{ match.i.team.name }}</td>
<td>{{ match.i.games.played }}</td>
<td>{{ match.i.games.win.total }}</td>
<td>{{ match.i.games.lose.total }}</td>
<td>{{ match.i.games.win.percentage }}</td>
</tr>
{% endfor %}
Assuming, that you only want to show the no. of iteration of instances.
Remove i
from every field in between.
Upvotes: 1