I have a need for counting the standings of a sport team based on the total points they have their division.
Each team has a total points and division assigned to them.
views.py
def seasonstandings(request):
divisions = Team.objects.order_by().values_list('division__name',flat=True).distinct()
stats = WeeklyStats.objects.values('player__team__team_name').annotate(
team=F('player__team__team_name'),
points = Sum('finishes'),
division = F('player__team__division__name')
).order_by('division','-points')
return render(request, 'website/seasonstandings.html', {
'divisions_and_stats': [[division, [stat for stat in stats if stat.division == division]] for division in divisions]
})
My Django template code is as follows:
seasonstandings.html
{% for division in divisions %}
<h4>{{ division }}</h4>
<table class="table">
<tr>
<td align="center">Place</td>
<td>Team</td>
<td align="center">Points</td>
</tr>
{% for stat in stats %}
{% if stat.division == division %}
<tr>
<td width="10%" align="center">{{ forloop.counter}}</td>
<td width="70%">{{ stat.team }}</td>
<td width="20%" align="center">{{ stat.points }}</td>
</tr>
{% endif %}
{% endfor %}
</table>
{% endfor %}
The problem right now is that say I have 6 teams and 3 are in Division A and 3 are in Division B.
Because it is separating the teams based on division it is showing the forloop.counter as 1 through 6 on the first forloop for divisions. What I want it to do is only do the forloop counter for the nested forloop ( the second one for stats) so that it shows places 1 through 3 for Division A and 1 through 3 for Division B.
The results I am getting are:
Division A
Place Team Points
1 Timberlea Beverage Room 1 7
3 Lakeside Legion 1 4
4 Bedford Legion 3
Division B
Place Team Points
2 Lakeside Legion 2 4
5 Purcells Cove Club 1 2
6 Army Navy Air-Force Club None
When I am hoping to get get the results to look like this:
Division A
Place Team Points
1 Timberlea Beverage Room 1 7
2 Lakeside Legion 1 4
3 Bedford Legion 3
Division B
Place Team Points
1 Lakeside Legion 2 4
2 Purcells Cove Club 1 2
3 Army Navy Air-Force Club None
After some more looking into this, I think it is my IF Statement causing the issue where it will skip a record if the division does not match. I can't think of a way to get it to count the numbers in order.