试图计算视图中的总数 - 在模板中显示空白

时间:2012-01-20 01:58:23

标签: python mysql django

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

views.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,但是在获得金额后我没有获得任何完全空白的结果信息:

谢谢!

1 个答案:

答案 0 :(得分:2)

您的视图功能中有两个返回。第二个永远不会到达。将其更改为:

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
     })