def post(self, request, *args, **kwargs): """ Handles POST request. :param request: request object. :param args: additional arguments. :param kwargs: additional key-value arguments. :return: HTTP response. """ user = User.objects.get(pk=kwargs['pk']) user_form = UserEditForm(self.request.POST, self.request.FILES, instance=user) profile_form = UserProfileEditForm( self.request.POST, instance=user.profile, ) if user_form.is_valid() and profile_form.is_valid(): with transaction.atomic(): user_form.save() profile_form.save() return HttpResponseRedirect(reverse('admin:users')) context = self.get_context_data() context['user_form'] = user_form context['profile_form'] = profile_form return render(request, self.template_name, context)
def form_valid(self, form, *args, **kwargs): profile_form = UserProfileEditForm( data=self.request.POST, files=self.request.FILES, instance=self.request.user.userprofile) if profile_form.is_valid(): form.save() profile_form.save() return HttpResponseRedirect(self.success_url) return render(self.request, self.template_name, { "form": form, "profile_form": profile_form })
def edit(request): title = 'редактирование' if request.method == 'POST': edit_form = UserProfileEditForm(request.POST, request.FILES, instance=request.user) profile_form = UserProfileEditForm(request.POST, instance=request.user.shopuserprofile) if edit_form.is_valid() and profile_form.is_valid(): edit_form.save() return HttpResponseRedirect(reverse('auth:edit')) else: edit_form = UserProfileEditForm(instance=request.user) profile_form = UserProfileEditForm( instance=request.user.shopuserprofile ) content = { 'title': title, 'edit_form': edit_form, 'profile_form': profile_form } return render(request, 'authapp/edit.html', content)