def index(request): if request.method == 'POST': if 'delete' in request.POST: player_list = Player.objects.order_by('-last_name') delete_player = player_list.get(pk=request.POST['delete-player']) delete_player.delete() player_list = Player.objects.order_by('-last_name') new_form = AddPlayerForm() context = {'player_list': player_list, 'form': new_form} render(request, 'player/index.html', context) else: form = AddPlayerForm(request.POST) if form.is_valid(): new_player = Player() new_player.first_name = form.cleaned_data['first_name'] new_player.last_name = form.cleaned_data['last_name'] new_player.number = form.cleaned_data['number'] new_player.save() new_form = AddPlayerForm() player_list = Player.objects.order_by('-last_name') context = {'player_list': player_list, 'form': new_form} render(request, 'player/index.html', context) form = AddPlayerForm() player_list = Player.objects.order_by('-last_name') context = {'player_list': player_list, 'form': form} return render(request, 'player/index.html', context)
def detail(request, team_id): team = get_object_or_404(Team, pk=team_id) if request.method == 'POST': if 'add-available' in request.POST: add_player = Player.objects.get(pk=request.POST['add-player']) team.players.add(add_player) new_form = AddPlayerForm() team_players = team.players.all() all_players = Player.objects.all() available_players = [] for player in all_players: if player not in team_players: available_players.append(player) context = {'team': team, 'form': new_form, 'available_players': available_players } return render(request, 'team/detail.html', context) else: form = AddPlayerForm(request.POST) if form.is_valid(): new_player = Player() new_player.first_name = form.cleaned_data['first_name'] new_player.last_name = form.cleaned_data['last_name'] new_player.number = form.cleaned_data['number'] new_player.save() team.players.add(new_player) new_form = AddPlayerForm() team_players = team.players.all() all_players = Player.objects.all() available_players = [] for player in all_players: if player not in team_players: available_players.append(player) context = {'team': team, 'form': new_form, 'available_players': available_players } return render(request, 'team/detail.html', context) else: form = AddPlayerForm() team_players = team.players.all() all_players = Player.objects.all() available_players = [] for player in all_players: if player not in team_players: available_players.append(player) context = {'team': team, 'form': form, 'available_players': available_players } return render(request, 'team/detail.html', context)