def new_league(request): if request.method == 'POST': # If the form was submitted... form = LeagueCreationForm(request.POST) # Make a form bound to the POST data if form.is_valid(): new_league = form.save(commit=False) new_league.commish = request.user new_league.save() FantasyTeam.objects.create(manager=request.user, league=new_league) return redirect(reverse('drafter.views.league', kwargs={ 'league_id': new_league.id })) # TODO: redirect has implicit reverse else: form = LeagueCreationForm() # Unbound form return render(request, 'drafter/leagues/new.html', { 'form': form })
def test_league_creation(self): test_league_data = {'name': 'test league', 'public': 'true', 'size': '12'} # Test redirect without log-in response = self.client.get(reverse('drafter.views.new_league')) # Target url = www.site.com/?next=/leagues/new/, determined by reverse look-up self.assertRedirects(response, reverse('drafter.views.index')+'?next='+reverse('drafter.views.new_league')); # Test login self.assertTrue(self.client.login(username='******', password='******')) # Test form presence response = self.client.get(reverse('drafter.views.new_league')) self.assertTrue('form' in response.context) # TEst form fields form = LeagueCreationForm(data = test_league_data) self.assertTrue(form.is_valid()) # Test league creation via form post and redirect form.cleaned_data['csrfmiddlewaretoken'] = [self.client.cookies['csrftoken'].value] response = self.client.post(reverse('drafter.views.new_league'), form.cleaned_data) test_league_obj = League.objects.get(name=test_league_data['name']) self.assertRedirects(response, reverse('drafter.views.league', kwargs={'league_id': test_league_obj.id-1 }), target_status_code=302)
def new_league(request): if request.method == 'POST': # If the form was submitted... form = LeagueCreationForm( request.POST) # Make a form bound to the POST data if form.is_valid(): new_league = form.save(commit=False) new_league.commish = request.user new_league.save() FantasyTeam.objects.create(manager=request.user, league=new_league) return redirect( reverse('league', kwargs={'league_id': new_league.id})) else: form = LeagueCreationForm() # Unbound form return render(request, 'drafter/leagues/new.html', {'form': form})