gpt4 book ai didi

python - 尝试计算 View 总数 - 在模板中显示空白

转载 作者:行者123 更新时间:2023-11-29 09:00:00 26 4
gpt4 key购买 nike

我正在尝试计算 For 循环中每个 user_name 赢得的总金额,并将其显示在 Amount Won: 之后。但是,当我运行下面的代码时,赢得金额后没有任何显示: - 它完全是空白的。我什至尝试将player__user_name更改为=单个user_name,但它仍然显示空白。任何帮助将不胜感激。

View .py

def index(request):
latest_player_list = Player.objects.all().order_by('id')[:20]
return render_to_response('stakeme/index.html', {'latest_player_list': latest_player_list})
total_amount_won = Stakes.objects.filter(player__user_name).aggregate(Sum('amount_won'))
return total_amount_won

index.html

<h1> Players </h1>

{% if latest_player_list %}
<ul>
{% for player in latest_player_list %}
<li><a href="/stakeme/{{ player.id }}/">{{ player.user_name }} </a><br>Total Won: {{ total_amount_won }}
</li>
{% endfor %}
</ul>
<br>
{% else %}
<p>No players are available.</p>
{% endif %}

<h3><a href="/stakeme/new/">New Player</a></h3>

我不确定我的计算是否会获取 For 循环中的每个 user_name,但在“赢得金额”后,我没有得到任何可用于处理完全空白结果的信息:

谢谢!

最佳答案

您的 View 函数中有两个返回值。第二个永远达不到。将其更改为:

def index(request):
latest_player_list = Player.objects.all().order_by('id')[:20]
total_amount_won = Stakes.objects.filter(player__user_name).aggregate(Sum('amount_won'))
return render_to_response('stakeme/index.html', {
'latest_player_list': latest_player_list,
'total_amount_won': total_amount_won
})

关于python - 尝试计算 View 总数 - 在模板中显示空白,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8935894/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com