def new_profile(request): if request.method == 'POST': form = ProfileForm(request.POST) if form.is_valid(): create_profile(request, form) else: form = ProfileForm() return render(request, 'accounts/profile_new.html', { 'form': form, })
def settings(request): user = request.user if request.method == 'GET': form = ProfileForm(instance=user) return render(request, 'settings.html', locals()) form = ProfileForm(request.POST, instance=user) if form.is_valid(): form.save(commit=True) messages.success(request, "设置已更新") return render(request, 'settings.html', {"form": form}) else: return render(request, 'settings.html', {"form": form})
def update_profile(request): """ This allows a user to update his or her profile. """ profile_kwargs = { 'instance': request.user.profile, 'prefix': 'profile', } email_kwargs = { 'instance': request.user, 'prefix': 'email', } if request.method == 'POST': if profile_kwargs['prefix'] in request.POST: profile_form = ProfileForm(request.POST, request.FILES, **profile_kwargs) if profile_form.is_valid(): profile_form.save() messages.success(request, "Your profile has been successfully " "updated") return redirect("accounts:edit_profile") email_form = EmailForm(**email_kwargs) elif email_kwargs['prefix'] in request.POST: email_form = EmailForm(request.POST, **email_kwargs) if email_form.is_valid(): email_form.save() messages.success(request, "Your email address has been " "successfully updated.") return redirect("accounts:edit_profile") profile_form = ProfileForm(**profile_kwargs) else: profile_form = ProfileForm(**profile_kwargs) email_form = EmailForm(**email_kwargs) return render_response(request, "accounts/edit-profile.html", { 'profile_form': profile_form, 'email_form': email_form, })
def profile(request, *args, **kwargs): profilepage = True # Create the basic forms which will be rendered in get requests user = request.user try: userprofile = UserProfile.objects.get(user = user) #profileform = ProfileForm(instance=userprofile, instance_user=user) except: userprofile = UserProfile(user = user) profileform = ProfileForm(instance=userprofile) if request.method == 'POST': postdata = request.POST.copy() if 'profileform' in postdata: # SIGNUP FORM profileform = ProfileForm(postdata, instance=userprofile) if profileform.is_valid(): # If the form is valid, save the user using the inbuilt function profileform.save() messages.success(request,'<strong>Done!</strong> Your account information was successfully saved !',extra_tags='alert-success') return HttpResponseRedirect(reverse('profile')) else: print "form errors" print profileform.errors if 'passwordform' in postdata: # SIGNUP FORM if postdata['password'] == postdata['confirm_password']: user.set_password(postdata['password']) user.save() print postdata['password'] else: print postdata['password'], postdata['confirm_password'] passwordform_errors = True show_passwordform = True if 'show_passwordform' in request.session: show_passwordform = request.session['show_passwordform'] del request.session['show_passwordform'] return render_to_response('pages/profile.html', locals(), context_instance= global_context(request))