I have a Player model, an Event model, and a Pick model. Theoretically, a Player should submit a Pick for each Event, but this doesn’t always happen and there may be times when a Pick is not yet submitted but will be in the following days.
I have a table displaying each Player’s Picks for each Event. Table header row loops through Events, then I have a regroup
and loop through each Player’s Picks for each table row.
Below is an image of how I’d like it work. When a Pick is missing, don’t print a Pick value, but skip that cell (or show a useful message to the Player):
My template is pasted below, but when a Pick is missing, there aren’t enough items in the queryset to match the number of table headers, and since template language is intentionally a bit naive (ie I can’t break a loop, or do some other features) I am having difficulty coming up with a way to arrange this template so that an empty table cell is added.
This is what my table resembles – Player 2’s picks are out of alignment. The orange cells don’t match the header Event, and the last red cell is empty since the loop doesn’t have the same number of Picks as Events:
Here’s my template code. I have comments near the lines that are important:
<!-- Regroup picks by player -->
{% regroup league_picks by player as player_picks %}
<h1>Picks are {{ picks_locked|yesno:'Locked,Unlocked' }}</h1>
<table class="result-table">
<thead>
<tr>
<th class="player">Player</th>
<th>Wins</th>
{% for event in events %}
{% if event.current_status.status.type.state != 'pre' %}
<th class="live-score">
{% with home=event.current_status.competitions.0.competitors.0 away=event.current_status.competitions.0.competitors.1 %}
<span>{{away.team.abbreviation}} {{away.score}}</span>
<br>
<span>{{home.team.abbreviation}} {{home.score}}</span>
<br>
<span>{{event.current_status.status.type.shortDetail}}</span>
{% endwith %}
</th>
{% else %}
<th>{{event.short_name}}</th>
{% endif %}
{% endfor %}
</tr>
</thead>
<tbody>
{% for player, picks in player_picks %}
<tr>
<td class="player">
{% if events|length == picks|length %}
<span class="picks-completed">
{% else %}
<span class="picks-not-completed">
{% endif %}
{{player}}
</span>
</td>
<td>{% player_wins_by_week season week player %}</td>
<!-- Looping through picks -->
{% for pick in picks %}
<!-- I could loop events also, and use pick.event.id == pick.id, but I can't break the loop -->
{% if pick.result == 'WIN' %}
<td class="win">{{pick.pick_team.short_name}}</td>
{% elif pick.result == 'LOSS' %}
<td class="loss">{{pick.pick_team.short_name}}</td>
{% else %}
{% if picks_locked %}
<td class="{% if pick|pick_status == pick.pick_team.id %}winning{% endif %}">{{pick.pick_team.short_name}}</td>
{% else %}
<td>{{pick.event.status}}</td>
{% endif %}
{% endif %}
{% endfor %}
</tr>
{% endfor %}
</tbody>
</table>