def post(self, request): form = ProfileForm(request.POST, request.FILES) if form.is_valid(): profile = form.save(commit=False) profile.user = request.user profile.save() messages.success(request, '更新完了してもうたけど!!') return render(request, 'mysite/mypage.html', self.context)
def mypage(request): context = {} if request.method == 'POST': form = ProfileForm(request.POST) if form.is_valid(): profile = form.save(commit=False) profile.user = request.user profile.save() messages.success(request, '更新完了') return render(request, 'mysite/mypage.html', context)
def mypageview(request): print(dir(request.user)) context = {'form': ProfileForm(instance=request.user.profile)} if request.method == 'POST': form = ProfileForm(request.POST) if form.is_valid(): profile = form.save(commit=False) profile.user = request.user profile.save() messages.success(request, 'プロフィールの更新が完了しました') return render(request, 'mysite/register/mypage.html', context) else: return render(request, 'mysite/register/mypage.html', context)
def create_user(request): if request.method == "POST": #import pdb;pdb.set_trace() form = UserForm(request.POST) personal_form = ProfileForm(request.POST) if form.is_valid(): #new_user = User.objects.create_user(**form.cleaned_data) #login(new_user) # redirect, or however you want to get to the main view return HttpResponseRedirect('user.html') else: form = UserForm() personal_form = PersonalForm() profile_form = ProfileForm() return render(request, 'user.html', {'form': form, 'form1':personal_form, 'form2':profile_form})
def dispatch(self, request, *args, **kwargs): form = ProfileForm(instance=self.get_profile(request.user)) if request.method == 'POST': form = ProfileForm(request.POST, request.FILES, instance=self.get_profile(request.user)) if form.is_valid(): form.instance.user = request.user form.save() messages.success(request, u"Profile is succesfully updated") return redirect(reverse("profile")) return render(request, self.template_name, {'form': form})
def update_profile(request): """ :param request: :return: """ if request.method == 'POST': user_form = UserForm(request.POST, instance=request.user) profile_form = ProfileForm(request.POST, instance=request.user.profile) if user_form.is_valid() and profile_form.is_valid(): user_form_data = user_form.cleaned_data profile_form_data = profile_form.cleaned_data profile_ = Profile.objects.get(user__username=request.user) user_ = User.objects.get(username=request.user) pre_form_change_email = user_.email pre_form_change_phone = profile_.phone_number user_form.save() profile_form.save() profile_ = Profile.objects.get(user=request.user) profile_.profile_filled = True if pre_form_change_email != user_form_data['email']: profile_.email_verification_status = 0 # Here we have to set a bit in User model that shows we have to confirm user's email if pre_form_change_phone != profile_form_data['phone_number']: profile_.phone_verification_status = 0 profile_.profile_filled = True profile_.save() return redirect('profile') else: return redirect('update_profile') else: profile_ = Profile.objects.get(user=request.user) user_form = UserForm(instance=request.user) profile_form = ProfileForm(instance=request.user.profile) return render( request, 'registration/profile.html', { 'user_form': user_form, 'profile_form': profile_form, 'isEmailVerified': profile_.email_verification_status, 'number_of_items': get_number_of_items_in_cart(request), 'profile_filled': profile_.profile_filled, })