tags:

views:

52

answers:

1

I have the following models defined:

class Player(models.Model):
    Team = models.ForeignKey(Team)
    Name = models.CharField(max_length=200)
    Position = models.CharField(max_length=3)
    ... snip ...

What I would like to output in a view is a list of players who are in the team with id = 1.

I have tried things such as:

{% for player in userTeam.userTeamSquad %}
       <tr><td>{{ player.Name }}</td><td> {{ player.Position }}</td></tr>
{% endfor %}

But can't get it right.

+2  A: 

You need a view that looks something like this:

def players(request):
    players_in_team_one = Player.objects.filter(Team__pk = 1)
    return render_to_response('players.html', {'players': players_in_team_one})

and you can loop through it like this in players.html:

{% for player in players %}
       <tr><td>{{ player.Name }}</td><td> {{ player.Position }}</td></tr>
{% endfor %}

p.s. As a matter of style, it's more standard to use all_lowercase_names_with_underscores as field names.

Dominic Rodger
Thanks, I will adopt that style also.
barryjenkins