Dan Hoerst
Dan Hoerst

Reputation: 6320

Attempting to calculate total in views - displaying blank in template

I'm trying to calculate the total amount won for each user_name in the For loop and display it after Amount Won:. However, when I run the code below, nothing displays after Amount Won: - it's completely blank. I've even tried changing the player__user_name to = a single user_name and it still shows blank. Any help would be appreciated.

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>

I'm not sure if my calculation will even pick up each user_name in the For loop, but I'm not getting any information to work with with a completely blank result after Amount Won:

Thanks!

Upvotes: 0

Views: 38

Answers (1)

Martin
Martin

Reputation: 4330

There are two returns in your view function. The second one is never reached. Change it to:

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

Upvotes: 2

Related Questions