def register(request): """ Registration page view """ registered = False # If form submitted if request.method == 'POST': user_form = UserForm(data=request.POST) player_form = PlayerForm(data=request.POST) # Validate forms if user_form.is_valid() and player_form.is_valid(): # Process user form data user = user_form.save() # Hash password and insert user into db user.set_password(user.password) user.save() # Process player form data player = player_form.save(commit=False) # Create relation from player to user # and insert player into db player.user = user player.save() registered = True # Accessing register page for first time else: user_form = UserForm() player_form = PlayerForm() # Create context dict for page rendering context = { 'user_form': user_form, 'player_form': player_form, 'registered': registered, } return render(request, 'layup/register.html', context)
def edit_player(request, player_url): """ Player edit profile view """ if request.user.username != player_url: raise Http404 #Check for valid player user = get_object_or_404(User, username=player_url) player = get_object_or_404(Player, user=user) # If form submitted if request.method =='POST': user_form = EditUserForm(request.POST, instance=user) player_form = PlayerForm(request.POST, instance=player) if user_form.is_valid() and player_form.is_valid(): # Process user update user = user_form.save() user.set_password(user.password) user.save() # Process player form data player = player_form.save() player.save() return HttpResponseRedirect( '/layup/player/%s/' % player_url ) # Accessing register page for first time else: user_form = EditUserForm(instance=user) player_form = PlayerForm(instance=player) # Create context dict for page rendering context = { 'user_form': user_form, 'player_form': player_form, 'player': player, } return render(request, 'layup/edit_player.html', context)