def user_profile(request): if request.method == 'POST': # instance подтягивает данные, вместо создания нового объекта # request.user - текущий залогиненый пользователь profile_form = ShopUserUpdateForm(request.POST, request.FILES, instance=request.user) user_profile = ShopUserProfileUpdateForm( request.POST, request.FILES, instance=request.user.shopuserprofile) if profile_form.is_valid() and user_profile.is_valid(): profile_form.save() # user_profile сохранится автоматически, так как используется @receiver в методе ниже # user_profile.save() # вариант сохранения без @receiver(post_save, sender=ShopUser) return HttpResponseRedirect(reverse('main:index')) else: profile_form = ShopUserUpdateForm(instance=request.user) user_profile = ShopUserProfileUpdateForm( instance=request.user.shopuserprofile) context = { 'page_title': 'профиль', 'form': profile_form, 'user_profile_form': user_profile, } return render(request, 'authapp/user_profile.html', context)
def update(request): if request.method == 'POST': form = ShopUserUpdateForm(request.POST, request.FILES, instance=request.user) profile_form = ShopUserProfileUpdateForm(request.POST, instance=request.user.shopuserprofile) if form.is_valid() and profile_form.is_valid(): form.save() # profile_form.save() # костомизировал - подтверждение успешного изменения # context = { 'title': 'Chenge personal data', 'form': form, 'profile_form': profile_form, 'confirmation': 'Changed successfully' } return render(request, 'authapp/update.html', context) else: form = ShopUserUpdateForm(instance=request.user) profile_form = ShopUserProfileUpdateForm( instance=request.user.shopuserprofile) context = { 'title': 'Chenge personal data', 'form': form, 'profile_form': profile_form, } return render(request, 'authapp/update.html', context)
def update(request): title = 'редактирование' if request.method == 'POST': form = ShopUserUpdateForm(request.POST, request.FILES, instance=request.user) profile_form = ShopUserProfileUpdateForm(request.POST, instance=request.user.shopuserprofile) if form.is_valid() and profile_form.is_valid(): form.save() return HttpResponseRedirect(reverse('auth:update')) else: form = ShopUserUpdateForm(instance=request.user) profile_form = ShopUserProfileUpdateForm(instance=request.user.shopuserprofile) context = { 'page_title': title, 'form': form, 'profile_form': profile_form } return render(request, 'authapp/update.html', context)
def update(request): if request.method == 'POST': form = ShopUserUpdateForm(request.POST, request.FILES, instance=request.user) profile_form = ShopUserProfileUpdateForm( request.POST, request.FILES, instance=request.user.shopuserprofile) if form.is_valid() and profile_form.is_valid(): form.save() return HttpResponseRedirect(reverse('auth:update')) else: form = ShopUserUpdateForm(instance=request.user) profile_form = ShopUserProfileUpdateForm( instance=request.user.shopuserprofile) context = { 'title': 'профиль пользователя', 'form': form, 'profile_form': profile_form, } return render(request, 'authapp/update.html', context)
def user_profile(request): if request.method == 'POST': user = ShopUserUpdateForm(request.POST, request.FILES, instance=request.user) user_profile = ShopUserProfileUpdateForm( request.POST, request.FILES, instance=request.user.shopuserprofile) if user.is_valid() and user_profile.is_valid(): user.save() # user_profile.save() return HttpResponseRedirect(reverse('main:index')) else: user = ShopUserUpdateForm(instance=request.user) user_profile = ShopUserProfileUpdateForm( instance=request.user.shopuserprofile) context = { 'page_title': 'профиль', 'form': user, 'user_profile_form': user_profile } return render(request, 'authapp/profile.html', context)