def post(self, request, *args, **kwargs): login_form = DynamicLoginPostForm(request.POST) dynamic_login = True banners = Banner.objects.all()[:3] if login_form.is_valid(): mobile = login_form.cleaned_data['mobile'] existed_users = UserProfile.objects.filter(mobile=mobile) if existed_users: user = existed_users[0] else: user = UserProfile(username=mobile) password = generate_random(10, 2) user.set_password(password) user.mobile = mobile user.save() login(request, user) next = request.GET.get('next', '') if next: return HttpResponseRedirect(next) return HttpResponseRedirect(reverse('index')) else: d_form = DynamicLoginForm() return render( request, 'login.html', { 'login_form': login_form, 'd_form': d_form, 'dynamic_login': dynamic_login, 'banners': banners })
def post(self, request, *args, **kwargs): login_form = DynamicLoginPostForm(request.POST) dynamic_login = True if login_form.is_valid(): #没有注册账号依然可以登陆 mobile = login_form.cleaned_data["mobile"] # code = login_form.cleaned_data["code"] existed_users = UserProfile.objects.filter(mobile=mobile) if existed_users: user = existed_users[0] else: #新建用户 user = UserProfile(username=mobile) password = generate_random(10, 2) user.set_password(password) user.mobile = mobile user.save() login(request, user) return HttpResponseRedirect(reverse("index")) else: d_form = DynamicLoginForm() return render( request, "login.html", { "login_form": login_form, "d_form": d_form, "dynamic_login": dynamic_login })
def post(self, request, *args, **kwargs): login_form = DynamicLoginPostForm(request.POST) dynamic_login = True banners = Banner.objects.all()[:3] if login_form.is_valid(): # 没有注册账号依然可以登录 mobile = login_form.cleaned_data['mobile'] # code = login_form.cleaned_data['code'] existed_users = UserProfile.objects.filter(mobile=mobile) if existed_users: user = existed_users[0] else: # 创建一个用户 user = UserProfile(username=mobile) password = generate_random(10, 2) user.set_password(password) user.mobile = mobile user.save() login(request, user) next = request.GET.get("next", "") if next: return HttpResponseRedirect(next) return HttpResponseRedirect(reverse("index")) else: d_form = DynamicLoginForm() return render(request, 'login.html', {"login_form": login_form, "d_form": d_form, "dynamic_login": dynamic_login, "banners":banners})
def post(self,request,*args,**kwargs): dynamic_login=True banners = Banner.objects.all()[:3] #验证手机号及手机验证码 login_form=DynamicLoginPostForm(request.POST) if login_form.is_valid(): #没有注册仍然可以使用 mobile=login_form.cleaned_data['mobile'] code=login_form.changed_data['code'] existed_user=UserProfile.objects.filter(mobile=mobile) if existed_user: user=existed_user[0] else: #新建用户 user=UserProfile(username=mobile) password = generate_random(10, 2) user.set_password(password) user.mobile=mobile user.save() login(request, user) next = request.GET.get("next", '') if next: return HttpResponseRedirect(next) return HttpResponseRedirect(reverse('index')) else: # 验证码错误 d_form=DynamicLoginForm() context = {'login_form': login_form,'dynamic_login':dynamic_login,'d_form':d_form, 'banners':banners} return render(request, 'login.html', context=context)
def post(self, request, *args, **kwargs): dynamic_login = True # 用于标识是否为动态登陆 login_form = DynamicLoginPostForm(request.POST) if login_form.is_valid(): # 没有账号依然可以登录 mobile = login_form.cleaned_data["mobile"] # 获取code进行redis认证,判断验证码是否一致.采用利用form进行验证redis,使得代码分离性更好-> form.py # 验证用户是否存在 existed_users = UserProfile.objects.filter(mobile=mobile) if existed_users: user = existed_users[0] login(request, user) else: # 新建一个用户 user = UserProfile(username=mobile) password = generate_random(12) # 生成密码 user.set_password(password) user.mobile = mobile user.save() login(request, user) next = request.GET.get("next") if next: return redirect(next, request) # 需要通过reverse跳转到index return redirect(reverse('index'), request) else: # 为了使得验证码得以显示,所以需要获取有验证码的form传递进来,验证码的form在普通的验证码字段 d_form = DynamicLoginForm() return render( request, "login.html", { "login_form": login_form, "dynamic_login": dynamic_login, "d_form": d_form, })
def post(self, request, *args, **kwargs): login_form = DynamicLoginPostForm(request.POST) dynamic_login = True banners = Banner.objects.all()[:3] if login_form.is_valid():#账号符合表单结构(forms中进行验证码匹配) #没有注册账号依然可以登录 email = login_form.cleaned_data["email"] existed_users = UserProfile.objects.filter(email=email)#寻找数据库中该邮箱用户 if existed_users:#如果账号存在 user = existed_users[0] else: #新建一个用户 user = UserProfile(username=email) password = generate_random(10, 2) user.set_password(password) user.email = email user.save() login(request, user)#登陆 next = request.GET.get("next", "") if next: return HttpResponseRedirect(next) return HttpResponseRedirect(reverse("index")) else: d_form = DynamicLoginForm() return render(request, "login.html", {"login_form": login_form, "d_form": d_form, "banners":banners, "dynamic_login":dynamic_login})
def post(self, request, *args, **kwargs): login_form = DynamicLoginPostForm(request.POST) dynamic_login = True if login_form.is_valid(): # 没有注册帐号依然可以登录 mobile = login_form.cleaned_data['mobile'] existed_user = UserProfile.objects.filter(mobile=mobile) if existed_user: user = existed_user[0] else: # 创建一个用户 user = UserProfile(username=mobile) password = gencrate_random(10, 2) user.set_password(password) user.mobile = mobile user.save() login(request, user) return HttpResponseRedirect(reverse('index')) else: d_form = DynamicLoginForm() # 生成图片验证码 return render( request, 'login.html', { 'login_form': login_form, 'd_form': d_form, 'dynamic_login': dynamic_login })
def post(self, request, *args, **kwargs): banner = Banner.objects.all()[:3] dynamic_login = True login_form = DynamicLoginPostForm(request.POST) if login_form.is_valid(): # 没有注册帐号也能登录 mobile = login_form.cleaned_data['mobile'] exist_users = UserProfile.objects.filter(mobile_phone=mobile) if exist_users: user = exist_users[0] login(request, user) else: # 新建一个用户 user = UserProfile(username=mobile) password = str(random.randint(100000, 999999)) user.set_password(password) user.mobile_phone = mobile user.save() login(request, user) next = request.GET.get('next', '') if next: return HttpResponseRedirect(next) return HttpResponseRedirect(reverse('index')) else: captcha_form = DynamicLoginForm() return render( request, 'login.html', { 'login_form': login_form, 'captcha_form': captcha_form, 'dynamic_login': dynamic_login, "banners": banner })
def post(self, request, *args, **kwargs): # 验证用户输入是否正确 login_form = DynamicLoginPostForm(request.POST) dynamic_login = True banners = Banner.objects.all()[:3] if login_form.is_valid(): mobile = login_form.cleaned_data["mobile"] # 查询用户是否存在 existed_users = UserProfile.objects.filter(mobile=mobile) if existed_users: # 存在——获取用户信息进行登录 user = existed_users[0] else: # 用户不存在,进行注册用户 user = UserProfile(username=mobile) # 随机生成密码 password = generate_random(10, 2) # 将随机密码进行加密 user.set_password(password) user.mobile = mobile user.save() # 进行登录并跳转至首页 login(request, user) return HttpResponseRedirect(reverse("index")) else: d_form = DynamicLoginForm return render(request, "login.html", {"login_form": login_form, 'd_form': d_form, "dynamic_login": dynamic_login, "banners": banners})
def post(self, request, *args, **kwarg): login_form = DynamicLoginPostForm(request.POST) dynamic_login = True if login_form.is_valid(): mobile = login_form.cleaned_data["mobile"] existed_users = UserProfile.objects.filter(mobile=mobile) if existed_users: user = existed_users[0] else: user = UserProfile(username=mobile) user.set_password("1234") user.mobile = mobile user.save() login(request, user) next = request.GET.get("next", "") if next: return HttpResponseRedirect(next) return HttpResponseRedirect(reverse("index")) else: d_form = DynamicLoginForm() return render( request, "login.html", { "login_form": login_form, "dynamic_login": dynamic_login, "d_form": d_form })
def post(self, request, *args, **kwargs): login_form = DynamicLoginPostForm(request.POST) dynamic_login = True if login_form.is_valid(): #没有注册账号依然可以登陆 mobile = login_form.cleaned_data["mobile"] code = login_form.cleaned_data["code"] existed_users = UserProfile.objects.filter(mobile=mobile) if existed_users: user = existed_users[0] else: #新建一个用户 user = UserProfile(username=mobile) password = generate_random((10, 2)) #生成用户首次登陆的随机密码 user.set_password(password) #生成该密码的秘文 user.mobile = mobile user.save() login(request, user) #使该用户登陆 next = request.GET.get("next", "") if next: return HttpResponseRedirect(next) return HttpResponseRedirect(reverse("index")) #跳转到首页 else: d_form = DynamicLoginForm() return render(request, "login.html", {"login_form": login_form, "d_form": DynamicLoginForm, "dynamic_login": dynamic_login}) #传递到html文件中
def post(self, request, *args, **kwargs): login_form = DynamicLoginPostForm(request.POST) dynamic_login = True if login_form.is_valid(): # 没有注册账号依然可以登录 mobile = login_form.cleaned_data["mobile"] existed_users = UserProfile.objects.filter(mobile=mobile) if existed_users: user = existed_users[0] else: # 新建用户 user = UserProfile(username=mobile) password = generate_random(10, 2) user.set_password(password) # 用set_password方法设置密码为密文 user.mobile = mobile user.save() login(request, user) return HttpResponseRedirect(reverse('login')) else: d_form = DynamicLoginForm() return render( request, 'login.html', { 'login_form': login_form, 'dynamic_login': dynamic_login, 'd_form': d_form })
def post(self, request, *args, **kwargs): login_form = DynamicLoginPostForm(request.POST) #设置一个变量、登录报错以后、让页面还停留在动态短信登陆页面 dynamic_login = True #如果短信验证码、验证成功 if login_form.is_valid(): # 没有账号可以登录 思路:先判断用户是否存在、如果存在 ...如果不存在先注册、给随机密码在登录、看以后代码 # 2、提取手机号 mobile = login_form.cleaned_data['mobile'] #提取手机号 existed_users = UserProfile.objects.filter(mobile=mobile) #如果手机号存在(用户存在) if existed_users: #把手机号给 login记录一下 user = existed_users[0] #如果手机号不存在 (用户不保存在) else: #添加用户 user = UserProfile(username=mobile) #添加用户名字 user.password = user #添加随机密码 # 3、生成随机数验证码、不怕爬虫去伪造 password = generate_random(10, 2) user.set_password(password) #保存新用户的手机号 user.mobile = mobile #保存密码 user.save() #保存用户的信息 login(request, user) #获取next next = request.GET.get('next', "") if next: # 直接跳转到http://127.0.0.1:8001/course/1/lesson/ 这个页面 return HttpResponseRedirect(next) # 跳转到主页 return HttpResponseRedirect(reverse("index")) else: #传递这个图片验证码的、表单是因为:如果短信验证码登录报错、重新实例化 验证码对象、传递到前端登陆页面、就不用刷新登陆页面 d_form = DynamicLoginForm() #如果短信验证码验证失败、会接受DynamicLoginPostForm 类的验证错误 return render( request, "login.html", { "login_form": login_form, "dynamic_login": dynamic_login, 'd_form': d_form })
def post(self, request, *args, **kwargs): login_form = DynamicLoginPostForm(request.POST) if login_form.is_valid(): mobile = login_form.cleaned_data["mobile"] existed_users = UserProfile.objects.filter(mobile=mobile) if existed_users: user = existed_users[0] else: # make new user user = UserProfile(username=mobile) password = generate_random(10, 2) # generate password user.set_password(password) user.mobile = mobile user.save() login(request, user) return HttpResponseRedirect(reverse("index")) else: return render(request, "login.html", {"login_form": login_form})
def post(self, request, *args, **kwargs): dynamic_login = True login_form = DynamicLoginPostForm(request.POST) # 校验手机号码(下面),和手机验证码(内嵌在DynamicLoginPostForm里面了) banners = Banner.objects.all()[:3] if login_form.is_valid(): # 去数据库查是否存在该用户 mobile = login_form.cleaned_data['mobile'] existed_user = UserProfile.objects.filter(mobile=mobile) # 如果用户存在,则直接登录 if existed_user: user = existed_user[0] # 如果用户不存在,则新建一个用户 else: # 默认用户名=手机号 user = UserProfile(username=mobile) # 使用明文密码不安全,所以先生存随机密码,再用AbstractUser自带的set_password来进行hash加密存储,防止被破解 password = generate_random(10, 2) user.set_password(password) # 别忘记存手机号 user.mobile = mobile # 最后对新建的对象进行保存,否则不会存进数据库 user.save() # 登录后,跳转首页 login(request, user) next = request.GET.get('next', '') if next: return HttpResponseRedirect(next) return HttpResponseRedirect(reverse('index')) # 手机号码或手机验证码,验证失败就跳回登录页面 # 需求,显示错误信息 else: # 生成d_form对象是为了当验证失败返回登录页面的时候,能够带上captcha字段 d_form = DynamicLoginForm() return render( request, 'login.html', { 'login_form': login_form, 'dynamic_login': dynamic_login, 'd_form': d_form, "banners": banners, })
def post(self, request): dynamic_login_flag = True login_form = DynamicLoginPostForm(request.POST) if login_form.is_valid(): mobile = login_form.cleaned_data.get('mobile') existed_users = UserProfile.objects.filter(mobile=mobile) if existed_users: user = existed_users[0] else: # 未查到注册信息 user = UserProfile(username=mobile) user.set_password(random_str.generate_random(10, 2)) user.mobile = mobile user.save() login(request, user) return HttpResponseRedirect(reverse('index')) else: d_form = DynamicLoginForm() return render( request, 'login.html', { 'login_form': login_form, 'dynamic_login_flag': dynamic_login_flag, 'd_form': d_form })