[Answered ]-Django Views Join Models Using Foreign Key

2👍

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.

Leave a comment