我已经有一个能正常运作的视图和模板来显示我的球员数据。我检索我的玩家获胜总数,然后创建一个名为points的新变量,然后创建(Wins * 3)并将此值添加到数组中。我将此数组传递给模板并显示。
我现在正在转换为基于类的视图,并且我不知道如何计算点。
以下工作正常:
def players(request):
# descending order player list by points
players = Player.objects.all().order_by('-points')
won = Match.objects.filter(won=True)
lost = Match.objects.filter(won=False)
tie = Match.objects.filter(won=None)
player_stats = []
for player in players:
print(player.name)
# print(player.preferredfoot)
won_matches = len(won.filter(player__name=player.name))
lost_matches = len(lost.filter(player__name=player.name))
tie_matches = len(tie.filter(player__name=player.name))
points = won_matches * 3 + tie_matches
player_stats.append([player.name, player.preferredfoot, won_matches, tie_matches, lost_matches, points, player.position, player.trait, player.goalsscored, player.assists, player.motmawards])
content = {
'player_stats': player_stats
}
return render(request, 'manager/players.html', content)
现在有了基于类的视图:-
class PlayerListView(ListView):
model = Player
template_name = 'manager/players.html'
context_object_name = 'player_stats' (used by my template)
我不明白如何在上面添加额外的数据,我只具有仅与Player对象相关的字段。在将上下文传递到模板之前,我需要向Player对象添加额外的值。
如何添加等效项
for player in players:
won_matches = len(won.filter(player__name=player.name))
won = Match.objects.filter(won=True)
...
points = won_matches * 3
进入我的context_object_name'player_stats'吗?
谢谢