def my_account_edit(request): profile = get_object_or_404(UserProfile, user=request.user) if request.method == 'POST': # change password password_form = PasswordChangeForm(request.user, request.POST) if password_form.is_valid(): user = password_form.save() update_session_auth_hash(request, user) messages.success(request, 'Your password was successfully updated!') return redirect('my_account_edit') # update user details user_form = UserEditForm(request.POST, instance=request.user) profile_form = UserProfileForm(request.POST, request.FILES, instance=profile) if profile_form.is_valid() and user_form.is_valid(): profile_form.save() user_form.save() messages.success(request, 'Account details successfully updated!') return redirect('my_account_edit') else: password_form = PasswordChangeForm(request.user) profile_form = UserProfileForm(instance=profile) user_form = UserEditForm(instance=request.user) context_dict = { 'password_form': password_form, 'profile_form': profile_form, 'user_form': user_form } return render(request, 'webapp/my-account-edit.html', context_dict)
def profile_view(request, username): if request.method == "POST": instance = get_object_or_404(UserProfile, user__username = username) form = UserProfileForm(request.POST or None, instance=instance) if form.is_valid(): form.save() #profile = UserProfile.objects.get(user__username = str(request.user)) #profile = form.save(commit = False) #profile.user = User.objects.get(username = str(request.user)) #profile.age = new_age #profile.bio = form.bio #profile.sex = form.sex #profile.picture = form.picture #profile.save() if username == str(request.user): editable = 1 else: editable = 0 form = UserProfileForm() account = UserProfile.objects.get(user__username = username) image = account.picture friends = account.friends name = str(account.user.first_name) + " " + str(account.user.last_name) bio = account.bio age = account.age sex = account.sex game_list = Game.objects.filter(joinees__username = username) games = serializers.serialize('json', game_list) return render(request, 'webapp/profile_view.html', {'image': image, 'editable': editable, 'username': username, 'friends': friends, 'name': name, 'bio': bio, 'age': age, 'sex': sex, 'games': games, 'form': form})
def register(request): if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(request.POST, request.FILES) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user profile.save() login(request, user, backend='django.contrib.auth.backends.ModelBackend') messages.success(request, 'Account successfully created!') return redirect('my_account') else: user_form = UserForm() profile_form = UserProfileForm() context_dict = {'user_form': user_form, 'profile_form': profile_form} return render(request, 'webapp/register.html', context_dict)
def register_user(request): context = RequestContext(request) registered = False if request.method == 'POST': user_form = UserForm(request.POST) profile_form = UserProfileForm(request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() profile = profile_form.save(commit=False) profile.user = user profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() return render_to_response('webapp/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered}, context)
def register(request): context = RequestContext(request) registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) #If the two forms are valid... if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() user.set_password(user.password) user.save() #Now we sort out the userProfile instance # Since we need to set the user attribute oursleves, we set commit=False # This delays saving the mdoel until we're ready to avoid integrity problems profile = profile_form.save(commit=False) profile.user = user if 'picture' in request.FILES: profile.picture = request.FILES['picture'] profile.save() registered = True else: print user_form.errors, profile_form.errors else: user_form = UserForm() profile_form = UserProfileForm() return render_to_response( 'webapp/register.html', {'user_form': user_form, 'profile_form': profile_form, 'registered': registered}, context)
def register(request, user_type): if request.user.is_authenticated(): return HttpResponseRedirect('/') context_dict = {} if request.method == 'POST': user_form = UserForm(data=request.POST) user_profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and user_profile_form.is_valid(): # User user = user_form.save() user.set_password(user.password) user.save() # UserProfile user_profile = user_profile_form.save(commit=False) user_profile.user = user if 'picture' in request.FILES: user_profile.picture = request.FILES['picture'] else: user_profile.picture = 'img/user_avatar.png' user_profile.save() # Fan/Band Profile if user_type == 'fan': profile = FanProfile.objects.get_or_create( profile=user_profile) else: profile = BandProfile.objects.get_or_create( profile=user_profile) # Auto login. user_login(request) return HttpResponseRedirect('/') else: user_form = UserForm() profile_form = UserProfileForm() context_dict['profile_form'] = profile_form context_dict['user_form'] = user_form return render(request, 'webapp/register.html', context_dict)