def edit_profile_action(request): print("Enter user/edit_profile_action") user = request.user profile = UserProfile.objects.get(user=user) # GET: view user profile if request.method == "GET": form = UserProfileForm(instance=profile) context = { 'profile': profile, 'form': form, 'user': user } return render(request, "edit-profile.html", context) # POST: update user profile edit_profile = UserProfile.objects.select_for_update().get(user=user) if request.FILES: print("User upload a new picture.") form = UserProfileForm(request.POST, request.FILES, instance=profile) else: print("User upload nothing.") form = UserProfileForm(request.POST, instance=profile) if not form.is_valid(): context = {'profile': profile, 'form': form} return render(request, 'edit-profile.html', context) edit_profile.update_time = timezone.now() form.save() profile = UserProfile.objects.get(user=user) context = { "user": user, "profile": profile } return render(request, "myprofile.html", context)
def register(request): #is registration successful registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() #hash password user.set_password(user.password) user.save() # commit - we need to set the user attribute our selves 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( request, 'user/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def register(request): if request.method == 'POST': u_form = UserRegistrationForm(request.POST) p_form = UserProfileForm(request.POST, request.FILES) if u_form.is_valid() and p_form.is_valid(): check = User.objects.filter( email=u_form.cleaned_data.get('email')).exists() if check is False: user = u_form.save() user.save() designation = p_form.cleaned_data.get('designation') if designation == 'TEACHER': group = Group.objects.get(name='TEACHERS') user.groups.add(group) else: group = Group.objects.get(name='STUDENTS') user.groups.add(group) profile = p_form.save(commit=False) profile.user = user if 'profile_pic' in request.FILES: profile.profile_pic = request.FILES['profile_pic'] profile.save() messages.success( request, "Your account has been created. Click Log In to Sign In") return redirect('index') else: messages.warning(request, 'Email already exists') return redirect('index') return render(request, 'ams/index.html')
def register(request): #is registration successful registered = False if request.method == 'POST': user_form = UserForm(data=request.POST) profile_form = UserProfileForm(data=request.POST) if user_form.is_valid() and profile_form.is_valid(): user = user_form.save() #hash password user.set_password(user.password) user.save() # commit - we need to set the user attribute our selves 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(request, 'user/register.html', { 'user_form': user_form, 'profile_form': profile_form, 'registered': registered })
def regist(request): if request.method == 'GET': return render(request, 'user/regist.html') else: # 读取注册的用户信息 form = UserProfileForm(request.POST) if form.is_valid(): user = form.save() # 验证没有任何问题,则写入数据库 # 清除上传图片的临时目录 photo = request.POST.get('photo') if photo: mvFilePath = mvImageFromTmp(photo) user.photo = mvFilePath user.save() # 更新目录 # 将当前注册成功的用户名和id写入到session中 request.session['login_user'] = json.dumps({'id': user.id, 'name': user.username, 'photo': user.photo}) return redirect('/') # 重定向到主页 else: errors_json = json.loads(form.errors.as_json()) return render(request, 'user/regist.html', locals()) # locals() 收集当前函数内部的可用对象,生成dict字典
def post(self, request, *args, **kwargs): user = request.user form_profile = UserProfileForm(request.POST, request.FILES, instance=user.profile) if form_profile.is_valid(): form_profile.save() return redirect('user:profile') else: return redirect('user:profile_update')
def user_profile(request): """ View for changing user's profile settings. """ info_form = UserInfoForm(instance=request.user) profile_form = UserProfileForm(request.POST, request.FILES, user=request.user) if profile_form.is_valid(): profile_form.save() return render(request, 'user/user-settings.html', {'active': 'profile', 'info_form': info_form, 'profile_form': profile_form})
def user_register(request): if request.method == "POST": user_form = RegistrationForm(request.POST) user_profile = UserProfileForm(request.POST) if user_form.is_valid() and user_profile.is_valid(): new_user = user_form.save(commit=False) new_user.password = user_form.cleaned_data["password1"] new_user.save() new_profile = user_profile.save(commit=False) new_profile.user = new_user new_profile.save() return HttpResponse("注册成功") else: print(user_form.is_valid()) print(user_profile.is_valid()) return HttpResponse("表单提交有误,注册失败") if request.method == "GET": user_form = RegistrationForm() user_profile = UserProfileForm() context = {"form": user_form, "profile": user_profile} return render(request, "user/register.html", context=context)
def editprofile(request): if request.method=="POST": form1=CreateUserForm(request.POST,instance=request.user) form2=UserProfileForm(request.POST,request.FILES,instance=request.user.profile,) if form1.is_valid() and form2.is_valid(): form1.save() form2.save() return redirect("user-profile") else: form1 = CreateUserForm(instance=request.user) form2 = UserProfileForm(instance=request.user.profile) context={ "form1":form1, "form2":form2 } return render(request,'authentication/editprofile.html',context)
def register(request): if request.method == 'POST': uf = UserForm(request.POST, prefix='user') upf = UserProfileForm(request.POST, prefix='userprofile') if uf.is_valid() * upf.is_valid(): user = uf.save(commit=False) user.set_password(uf.cleaned_data['password']) user.save() userprofile = upf.save(commit=False) userprofile.user = user userprofile.save() return redirect('login') else: uf = UserForm(prefix='user') upf = UserProfileForm(prefix='userprofile') return render(request, 'register.html', dict(userform=uf, userprofileform=upf))
def register(request): template = 'user/register.html' if request.method=='GET': return render(request, template, {'userForm':UserForm(), 'userProfileForm':UserProfileForm()}) # request.method == 'POST': userForm = UserForm(request.POST) userProfileForm = UserProfileForm(request.POST) if not (userForm.is_valid() and userProfileForm.is_valid()): return render(request, template, {'userForm':userForm, 'userProfileForm':userProfileForm}) user = userForm.save() user.set_password(user.password) user.save() userProfile = userProfileForm.save(commit=False) userProfile.user = user userProfile.save() messages.success(request, '歡迎註冊') return redirect(reverse('main:main'))
def register(request): if request.method== "POST": form=UserCreation(request.POST) if form.is_valid() : form.save() return redirect("index_page") if request.method=="POST": form1=UserProfileForm(request.POST) if form1.is_valid(): form1.save() return redirect("index_page") else: form = UserCreation() form1= UserProfileForm() context={ "form":form, "form1":form1 } return render(request,"authentication/register.html",context)
def register(request): 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.is_active = False user.save() profile = profile_form.save(commit=False) profile.user = user profile.save() # Sending confirmation email current_site = get_current_site(request) mail_subject = 'Account Activation' message = render_to_string( 'user/acc_active_email.html', { 'user': user, 'domain': current_site.domain, 'uid': urlsafe_base64_encode(force_bytes(user.id)), 'token': account_activation_token.make_token(user) }) to_email = user_form.cleaned_data.get('email') email = EmailMessage(mail_subject, message, to=[to_email]) email.send() # username= user_form.cleaned_data.get('username') # password=user_form.cleaned_data.get('password1') # user=authenticate(username=username,password=password) # login(request,user) return render(request, 'user/must_activate.html') else: user_form = UserForm() profile_form = UserProfileForm() context = {'user_form': user_form, 'profile_form': profile_form} return render(request, 'user/register.html', context)
def register(request): user_form = UserForm() user_profile_form = UserProfileForm() if request.method == 'POST': user_form = UserForm(request.POST) user_profile_form = UserProfileForm(request.POST) if user_form.is_valid() and user_profile_form.is_valid(): print('-----------------register feo-----------', user_form) user = user_form.save() user_profile_form = user_profile_form.save(commit=False) user_profile_form.user = user user_profile_form.save() return redirect('user_app:user') return render(request, 'register.html', { 'user_form': user_form, 'user_profile_form': user_profile_form })
def registerPage(request): if request.user.is_authenticated: return redirect('index') else: form = SignUpForm() profile_form = UserProfileForm(request.POST) if request.method =='POST': form = SignUpForm(request.POST) profile_form = UserProfileForm(request.POST) if form.is_valid() and profile_form.is_valid(): user = form.save() profile = profile_form.save(commit=False) profile.user=user profile.save() user = form.cleaned_data.get('username') messages.success(request, 'Profile was created succesfully for ' + user ) return redirect('login') category = Category.objects.all() context = {'category':category, 'form':form, 'profile_form':profile_form} return render(request, 'accounts/register.html', context)
def regist(request): if request.method == 'GET': return render(request, 'user/regist.html') else: # 读取用户信息 form = UserProfileForm(request.POST) print('--test1---') if form.is_valid(): # form.photo = mvImageFromTmp(form.photo) user = form.save() request.session['login_user'] = json.dumps({'id':user.id, 'name': user.username, 'photo':user.photo}) print('---test11') return redirect('/') #重定向到主页 else: print('--test2---') errors_json = json.loads(form.errors.as_json()) return render(request, 'user/regist.html', locals()) #locals 收集当前函数内部的所有可用对象,生成字典
def regist(request): if request.method == 'GET': return render(request, 'user/regist.html') else: # 读取用户信息 form = UserProfileForm(request.POST) print('--test1---') if form.is_valid(): # form.photo = mvImageFromTmp(form.photo) user = form.save() request.session['login_user'] = json.dumps({ 'id': user.id, 'name': user.username, 'photo': user.photo }) print('---test11') return redirect('/') #重定向到主页 else: print('--test2---') errors_json = json.loads(form.errors.as_json()) return render(request, 'user/regist.html', locals()) #locals 收集当前函数内部的所有可用对象,生成字典
def update_user_details(request, pk): user = User.objects.get(pk=pk) user_form = UserProfileForm(instance=user) ProfileInlineFormset = inlineformset_factory(User, UserProfile, form=InfoForm, extra=0, can_delete=False) formset = ProfileInlineFormset(instance=user) if request.user.is_authenticated() and request.user.id == user.id: if request.method == "POST": user_form = UserProfileForm(request.POST, request.FILES, instance=user) formset = ProfileInlineFormset(request.POST, request.FILES, instance=user) if user_form.is_valid(): created_user = user_form.save(commit=False) formset = ProfileInlineFormset(request.POST, request.FILES, instance=created_user) if formset.is_valid(): created_user.save() formset.save() return HttpResponseRedirect('/') return render(request, "account_update.html", { "noodle": pk, "noodle_form": user_form, "formset": formset, }) else: raise PermissionDenied
def edit_profile(request, username): if request.user.is_anonymous: messages.error(request, 'خطأ في تنفيذ العملية!') return redirect('home') context = { 'title': 'تعديل الحساب', } if request.method == 'POST': newProfile = UserProfileForm(data=request.POST) if newProfile.is_valid(): np = newProfile.save(commit=False) np.user = request.user np.save() messages.success(request, 'تم تحديث ملفك الشخصي!') return redirect('profile', request.user.username) elif request.user.profile.DoesNotExist: request.user.profile = UserProfileForm() context['form'] = UserProfileForm() else: context['form'] = request.user.profile return render(request, 'user/edit_profile.html', context)