def post(self, request): form = UserLoginForm(request.POST) message = '请检查填写的内容!' if form.is_valid(): username = form.cleaned_data.get('username_or_email') password = form.cleaned_data.get('password') # 如果符合邮件正则 if re.match(r'^[0-9a-zA-Z_.]+@[A-Za-z]+\.[a-zA-Z]+$', str(username)): email = username try: email_user = User.objects.get(email=email) except User.DoesNotExist: message = '邮件不存在' return render(request, 'registration/login.html', { 'form': form, 'message': message }) if email_user is not None: username = email_user.username user = authenticate(request, username=username, password=password) if user is not None: login(request, user) return redirect(reverse('core:MovieList')) return render(request, 'registration/login.html', { 'form': form, 'message': message })
def login(request): # get请求返回登录页面 if request.method == 'GET': return render(request, 'login.html') # POST if request.method == 'POST': # 拿到post请求的参数存在data里 data = request.POST # 创建一个类UserLoginForm # 将请求参数去给form表单做验证 form = UserLoginForm(data) # 校验结果:返回true表示校验成功 就是有user if form.is_valid(): # 使用随机标识符token user = auth.authenticate(username=form.cleaned_data.get('username'), password=form.cleaned_data.get('password')) if not user: # 没有user对象 表示验证密码不通过 return render(request, 'login.html', {'msg': '密码错误'}) else: # 实现登录 request, user等于登录系统用户对象 auth.login(request, user) return HttpResponseRedirect(reverse('user:index')) # 校验结果:返回false表示校验失败 就是没有user else: # 验证失败 返回错误信息给页面 # return render(request, 'login.html', {'errors': form.errors}) errors = form.errors return render(request, 'login.html', {'errors': errors})
def user_login(request): if request.method == 'POST': user_login_form = UserLoginForm(data=request.POST) if user_login_form.is_valid(): # .cleaned_data 清洗出合法数据 # Form不仅负责验证数据,还可以“清洗”它:将其标准化为一致的格式, # 这个特性使得它允许以各种方式输入特定字段的数据,并且始终产生一致的输出。 # 一旦Form使用数据创建了一个实例并对其进行了验证,就可以通过cleaned_data属性访问清洗之后的数据 data = user_login_form.cleaned_data # 检验账号、密码是否正确匹配数据库中的某个用户 # 如果均匹配则返回这个 user 对象 # authenticate()方法验证用户名称和密码是否匹配,如果是,则将这个用户数据返回。 user = authenticate(username=data['username'], password=data['password']) if user: # login()方法实现用户登录,将用户数据保存在session中 login(request, user) # 登陆成功,返回主页 return redirect("article:article_list") else: return HttpResponse("账号或密码输入有误。请重新输入~") else: return HttpResponse("账号或密码输入不合法") elif request.method == 'GET': user_login_form = UserLoginForm() data = {'user_login_form': user_login_form} return render(request, 'user/login.html', data) else: return HttpResponse("请使用GET或POST请求数据")
def login(request): next_url = request.GET.get('next', False) if request.user.is_authenticated(): return HttpResponseRedirect("/dashboard/") form = UserLoginForm() if request.method == 'POST': form = UserLoginForm(request.POST) if form.is_valid(): username = request.POST['username'].lower() password = request.POST['password'] new_user = authenticate(username=username, password=password) if new_user is not None: if new_user.is_active: user_login(request, new_user) if next_url: return HttpResponseRedirect(next_url) else: return HttpResponseRedirect( reverse("dashboard:dashboard")) else: messages.warning( request, 'Your account is not activeted, please activate your account!' ) else: messages.warning(request, 'Login email and password are incorrect!!') return render(request, "users/login.html", { 'form': form, 'next': next_url })
def test_user_login_form_valid(self): form_data = { 'email': self.email_valid, 'password': self.password_valid } form = UserLoginForm(data=form_data) self.assertTrue(form.is_valid())
def test_user_login_form_invalid_NULL_2(self): form_data = { 'email': self.email_invalid, 'password': self.password_invalid_NULL } form = UserLoginForm(data=form_data) self.assertFalse(form.is_valid())
def login(request): if request.method == 'GET': #GET请求返回登录页面 return render(request, 'login.html') if request.method == 'POST': #将请求参数给form表单做验证 data = request.POST form = UserLoginForm(data) #校验结果,返回true表示校验成功 if form.is_valid(): user = auth.authenticate( username=form.cleaned_data.get('username'), password=form.cleaned_data.get('password')) if not user: return render(request, 'login.html') #实现登录 auth.login(request, user) return HttpResponseRedirect(reverse('user:index')) else: #验证失败 errors = form.errors return render(request, 'login.html', {'errors': errors})
def test_valid_form(self): """Test valid form""" form = UserLoginForm({ "email": "*****@*****.**", "password": "******" }) self.assertTrue(form.is_valid())
def user_login(request): if request.method == "POST": form = UserLoginForm(request.POST) if form.is_valid(): username = request.POST['username'] password = request.POST['password'] # authenticating the user user = authenticate(username=username, password=password) # checking if user is already logged in if user: if user.is_active: login(request, user ) # pass the request and user authentication object return HttpResponseRedirect(reverse('blog-home')) else: messages.warning(request, "user is not active") else: messages.warning(request, "It's not valid username and password!") else: form = UserLoginForm() context = { 'form': form, } return render(request, 'user/user_login.html', context)
def login(request): if request.method == 'GET': # get请求返回登录页面 return render(request, 'login.html') if request.method == 'POST': # 将请求参数丢给form表单做检验 form = UserLoginForm(request.POST) # 校验结果, 返回true表示校验成功 if form.is_valid(): user = auth.authenticate( username=form.cleaned_data.get('username'), password=form.cleaned_data.get('password')) if not user: # 没有user对象, 表示验证密码不通过 return render( request, 'login.html', ) # 实现登录, request.user等于登录系统用户对象 auth.login(request, user) return HttpResponseRedirect(reverse('user:index')) else: # 验证失败, 返回错误信息给页面 errors = form.errors return render(request, 'login.html', {'errors': errors})
def login(request): if request.method == 'GET': # 返回登录页面 return render(request, 'login.html') if request.method == 'POST': # 获取所有的请求数据 data = request.POST # 将数据交给form表单验证 form = UserLoginForm(data) if form.is_valid(): # 如果验证通过就将数据保存到数据库 user = auth.authenticate(username=form.cleaned_data.get('username'), password=form.cleaned_data.get('password')) if not user: # 没有user对象,表示验证密码不通过 return render(request, 'login.html') # 实现登录,request.user等于登录系统用户对象 auth.login(request, user) return HttpResponseRedirect(reverse('user:index')) else: # 验证失败返回错误信息 errors = form.errors return render(request, 'login.html', {'errors': errors})
def post(self, request): form = UserLoginForm(data=request.POST) template = 'user/login.html' if form.is_valid(): user = form.get_user() login(request, user) return redirect('schemas') else: context = {'form': form} return render(request, template, context=context)
def login(request): if request.method == 'GET': return render(request, 'login.html') if request.method == 'POST': form = UserLoginForm(request.POST) if form.is_valid(): # 验证通过,进行会话 request.session['user_id'] = User.objects.filter( username=form.cleaned_data.get('username')).first().id return HttpResponseRedirect(reverse('goods:index')) return render(request, 'login.html', {'errors': form.errors})
def login(request): if request.method == 'GET': return render(request, 'login.html') if request.method == 'POST': form = UserLoginForm(request.POST) if form.is_valid(): request.session['account_number'] = form.cleaned_data[ 'account_number'] return HttpResponseRedirect(reverse('train:index')) errors = form.errors return render(request, 'login.html', {'errors': errors})
def register_post(request): """只接受post请求的注册接口""" if request.method == "POST": user = UserLoginForm(request.POST) # print(user) # 验证表单数据 if not user.is_valid(): # print(user.errors.get_json_data) # # print(user.) # # print(vaild) return JsonResponse(user.errors.get_json_data(), safe=False) user = UserLoginForm() return render(request, "user/register_post.html", {"user": user})
def post(self, request): user_login_form = UserLoginForm(request.POST) if user_login_form.is_valid(): user_login_form.save() username = user_login_form.cleaned_data.get('username') raw_password = user_login_form.cleaned_data.get('password1') user = authenticate(username=username, password=raw_password) login(request, user) return HttpResponseRedirect(reverse_lazy('blog:blog-list'))
def user_login(request): if request.method == 'POST': user_login_form = UserLoginForm(request.POST) if user_login_form.is_valid(): username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username,password=password) if user: login(request,user) return redirect("blog:blog") else: return render(request,'user/login.html',{'errmsg':"用户名或密码输入有误,请重新输入", 'username':username, 'password':password}) elif request.method == 'GET': return render(request,'user/login.html',{'errmsg':None})
def get(self, request): if request.user.is_authenticated: return redirect('schemas') form = UserLoginForm() template = 'user/login.html' context = {'form': form} return render(request, template, context=context)
def login(request): if request.method == 'GET': return render(request, 'login.html') if request.method == 'POST': form = UserLoginForm(request.POST) if form.is_valid(): user = auth.authenticate(username=form.cleaned_data['username'], password=form.cleaned_data['password']) if user: # request.user赋值,赋值为登录用户对象 auth.login(request, user) return HttpResponseRedirect(reverse('user:my_index')) else: return render(request, 'login.html') else: return render(request, 'login.html', {'errors': form.errors})
def login(request): """登录""" if request.method == 'GET': return render(request, 'login.html') if request.method == 'POST': # 使用cookie+session形式实现登陆 form = UserLoginForm(request.POST) if form.is_valid(): user = User.objects.filter( username=request.POST.get('username')).first() request.session['username'] = user.username ip = get_host_ip() LoginLog.objects.create(login_ip=ip, loginer=user) return HttpResponseRedirect(reverse('user:index')) else: return render(request, 'login.html', {'form': form})
def user_login(request): if request.method == 'POST': form = UserLoginForm(request.POST) if form.is_valid(): username = request.POST['username'] password = request.POST['password'] user = authenticate(username=username, password=password) if user: if user.is_active: login(request, user) return HttpResponseRedirect(reverse('sell:home')) else: return HttpResponse('User is not active') else: return HttpResponse('Invalid User') else: form = UserLoginForm() return render(request, 'login.html', {'form': form})
def login(request): if request.method == 'GET': return render(request, 'login.html') if request.method == 'POST': form = UserLoginForm(request.POST) if form.is_valid(): pwd = form.cleaned_data['pwd'] username = form.cleaned_data['username'] user = User.objects.filter(username=username).first() if check_password(pwd, user.password): # 校验成功 request.session['user_id'] = user.id return HttpResponseRedirect(reverse('goods:index')) else: err_pwd = '账号或密码错误' return render(request, 'login.html', {'err_pwd': err_pwd}) else: errors = form.errors return render(request, 'login.html', {'errors': errors})
def user_login(request): if request.method == 'GET': return render(request, 'login.html') else: user_form = UserLoginForm(request.POST) if user_form.is_valid(): username = user_form.cleaned_data['username'] password = user_form.cleaned_data['password'] user = authenticate(username=username, password=password) if user: if user.is_active == 1: login(request, user) return redirect(reverse('index')) else: return HttpResponse('账号还未激活!') else: return render(request, 'login.html', {'msg': '用户名或者密码错误'}) else: return render(request, 'login.html', {'user_form': user_form})
def register_post(request): """只接受post请求的注册接口""" if request.method == 'POST': user = UserRegisterForm(request.POST) # print(user) if not user.is_valid(): # valid = user.is_valid() return JsonResponse(user.errors.get_josn_data, safe=False) user = UserLoginForm() return render(request, 'user/register_post.html', {'user': user})
def post(self, request): form = UserLoginForm(self.POST()) if not form.is_valid(): return RestResponse.frontFail("参数错误!", form.errorsDict()) data = form.cleaned_data openId = wechatAuth.requestWechat(data["code"]) if not openId: return RestResponse.userFail("登录失败!") data['openId'] = openId data['loginTime'] = datetime.datetime.now() del data['code'] user = User.objects.filter(openId=openId).values('userId').first() if user: userId = data['userId'] = user['userId'] Data.updateData(User, data) else: user = Data.createData(User, data) userId = user.userId sn = Token.setSn(userId) return RestResponse.success("登录成功!", {"userId": userId, "sn": sn})
def login(request): if request.method == 'GET': return render(request, 'login.html') if request.method == 'POST': data = request.POST form = UserLoginForm(data) if form.is_valid(): # 表单验证用户名和密码 password = form.cleaned_data.get('password') username = form.cleaned_data.get('username') user = auth.authenticate(username=username, password=password) if user: # 实现登录,request.user等于登录系统的用户 auth.login(request, user) return HttpResponseRedirect(reverse('user:index')) else: return render(request, 'login.html') else: errors = form.errors return render(request, 'login.html', {'errors': errors})
def login(request): if request.method == 'GET': return render(request, 'login.html') if request.method == 'POST': form = UserLoginForm(request.POST) if form.is_valid(): user = User.objects.filter( username=form.cleaned_data.get('username')).first() password = form.cleaned_data.get('password') if check_password(password, user.password): # 验证成功 # django自带auth模块,签名token,会话上下文session # request.user request.session['user_id'] = user.id return HttpResponseRedirect(reverse('goods:index')) else: msg = '密码错误' return render(request, 'login.html', {'msg': msg}) else: return render(request, 'login.html', {'errors': form.errors})
def login(request): if request.method == 'GET': return render(request, 'login.html') if request.method == 'POST': data = request.POST form = UserLoginForm(data) if form.is_valid(): # 使用随机标识符也叫做签名token user = auth.authenticate(username=form.cleaned_data.get('username'), password=form.cleaned_data.get('password')) if user: # 登录, 向request.user属性赋值,默认为AnyouseUser,赋值为登录系统的用户对象 # 1. 向页面的cookie中设置sessionid值,(标识符) # 2. 向django_session表中设置对应的标识符 auth.login(request, user) return HttpResponseRedirect(reverse('user:index')) else: return render(request, 'login.html', {'msg': '密码错误'}) else: return render(request, 'login.html', {'errors': form.errors})
def login(request): if request.method == 'GET': return render(request, 'login.html') if request.method == 'POST': form = UserLoginForm(request.POST) if form.is_valid(): user = User.objects.filter( username=form.cleaned_data['username']).first() if user: if not check_password(form.cleaned_data['password'], user.password): msg = '密码错误!' return render(request, 'login.html', {'msg': msg}) else: request.session['user_id'] = user.user_id # add_goods_to_session(request) return HttpResponseRedirect(reverse('home:index')) else: msg = '用户不存在' return render(request, 'login.html', {'msg': msg}) else: return render(request, 'login.html', {'form': form})
def login_action(request): context = {} if request.method == "GET": return render(request, 'login.html', context) form = UserLoginForm(request.POST) context["loginForm"] = form # check login form validility if not form.is_valid(): print("User Login Form is invalid ...") return render(request, 'login.html', context) username, password = form.cleaned_data["username"], form.cleaned_data["password"] print("{0}: {1}".format(username, password)) user = authenticate(username=username, password=password) if user is None: print("User authentication failed ...") context["msg"] = "username or password does not exist." return render(request, 'login.html', context) print("User authentication success!") userProfile = user.myprofile # check activation if not userProfile.activated: print("User has not been activated") context["msg"] = "User has not been activated." return render(request, 'login.html', context) # check login information if not user: print("User logged in failed.") context["msg"] = "username and password do not match or exist." return render(request, 'login.html', context) login(request, user) print("User logged in successfully.") return redirect('/')