def edit_league(request, pk): league = get_object_or_404(League, pk=pk) if request.method == 'POST': form = LeagueForm(request.POST) if form.is_valid(): league.name = form.cleaned_data['name'] league.season = form.cleaned_data['season'] teams = form.cleaned_data['teams'] for team in league.teams.all(): to_delete = True for team2 in teams: if team == team2: to_delete = False break if to_delete: league.leagueteamrel_set.filter(team=team).delete() for team2 in teams: to_create = True for team in league.teams.all(): if team2 == team: to_create = False break if to_create: league_team_rel = LeagueTeamRel(league=league, team=team2) league_team_rel.save() league.save() return redirect('AthleticTeamApp:ShowLeague', league.id) else: form = LeagueForm(instance=league) return render(request, 'league/edit_form.html', {'league': league, 'form': form})
def create_league(request): if request.method == 'POST': form = LeagueForm(request.POST) if form.is_valid(): name = form.cleaned_data['name'] season = form.cleaned_data['season'] league = League(name=name, season=season) league.save() for team in form.cleaned_data['teams']: league_team_rel = LeagueTeamRel(league=league, team=team) league_team_rel.save() return redirect('AthleticTeamApp:ShowLeague', league.id) else: form = LeagueForm() return render(request, 'league/create_form.html', {'form': form})
def edit_league(request, pk): league = get_object_or_404(League, pk=pk) if request.method == 'POST': form = LeagueForm(request.POST) if form.is_valid(): league.name = form.cleaned_data['name'] league.season = form.cleaned_data['season'] teams = form.cleaned_data['teams'] for team in league.teams.all(): to_delete = True for team2 in teams: if team == team2: to_delete = False break if to_delete: league.leagueteamrel_set.filter(team=team).delete() for team2 in teams: to_create = True for team in league.teams.all(): if team2 == team: to_create = False break if to_create: league_team_rel = LeagueTeamRel(league=league, team=team2) league_team_rel.save() league.save() return redirect('AthleticTeamApp:ShowLeague', league.id) else: form = LeagueForm(instance=league) return render(request, 'league/edit_form.html', { 'league': league, 'form': form })