def test_invalid_login(self): login_credentials = [{'email':'', 'password':''}, {'email':'madhavbnk', 'password':''}, {'email':'', 'password':'******'}, {'email':'nonexistant_emailaddres', 'password':'******'}] form = UserLoginForm(login_credentials[0]) self.assertFalse(form.is_valid()) self.assertTrue(form.errors) self.assertTrue(form.errors.has_key('email')) self.assertTrue(form.errors.has_key('password')) form = UserLoginForm(login_credentials[1]) self.assertFalse(form.is_valid()) self.assertTrue(form.errors) self.assertTrue(form.errors.has_key('email')) self.assertTrue(form.errors.has_key('password')) form = UserLoginForm(login_credentials[2]) self.assertFalse(form.is_valid()) self.assertTrue(form.errors) self.assertTrue(form.errors.has_key('email')) form = UserLoginForm(login_credentials[3]) self.assertFalse(form.is_valid()) self.assertTrue(form.errors) self.assertTrue(form.errors.has_key('email')) self.assertEquals(form.errors.get('email')[0], 'Enter a valid e-mail address.') self.assertFalse(form.errors.has_key('password'))
def login(request, template="users/login.html"): """Displays the login form and handles the login action.""" if request.method == "POST": form = UserLoginForm(data=request.POST) if form.is_valid(): auth_login(request, form.get_user()) if request.session.test_cookie_worked(): request.session.delete_test_cookie() return HttpResponse( simplejson.dumps({ 'response': '/', 'result': 'success' })) else: response = {} for k, v in form.errors.items(): response[k] = v return HttpResponse( simplejson.dumps({ 'response': response, 'result': 'error' })) login_form = UserLoginForm(request) register_form = RegisterForm request.session.set_test_cookie() return direct_to_template(request, template, extra_context={ 'login_form': login_form, 'register_form': register_form })
def user_login(request): if request.method == 'GET': return render(request, 'user_login.html') else: # 获取表单信息 user_login_form = UserLoginForm(request.POST) # 判断表单信息 if user_login_form.is_valid(): username = user_login_form.cleaned_data['username'] password = user_login_form.cleaned_data['password'] checkcode = user_login_form.cleaned_data['checkcode'] user = UserProfile.objects.filter(username=username) # 判断用户是否注册 if user.exists(): user = user.first() code = request.session['code'] # 密码核对 if code.lower() == checkcode.lower(): if user: if check_password(password, user.password): request.user = user request.session['username'] = username return redirect(reverse('index')) else: return render(request, 'user_login.html', {'msg': '密码错误!'}) else: return render(request, 'user_login.html', {'msg': '验证码错误'}) else: return render(request, 'user_login.html', {'msg': '该用户不存在,请确认再登录'})
def user_login(request): if request.method == "POST": form = UserLoginForm(request.POST) if form.is_valid(): # import pdb; pdb.set_trace() username = request.POST.get("username", "") password = request.POST.get("password", "") # authenticate check username password incorect user = authenticate(username=username, password=password) if user: if user.is_active: # saves the user’s ID in the session, using Django’s session framework login(request, user) messages.success(request, f"Hi {user.username}") return redirect(reverse_lazy("blog:post_list")) else: return HttpResponse("user is not active") else: return HttpResponse("user is none") else: form = UserLoginForm() context = { "form": form, } return render(request, "registration/login.html", context)
def post(self, request): user_login_form = UserLoginForm(request.POST) if user_login_form.is_valid(): email = user_login_form.cleaned_data['email'] password = user_login_form.cleaned_data['password'] user = authenticate(username=email, password=password) if user: if user.is_start: login(request, user) a = UserMessage() a.message_man = user.id a.message_content = '欢迎登录' a.save() url = request.COOKIES.get('url', '/') ret = redirect(url) ret.delete_cookie('url') return ret else: return HttpResponse('请去邮箱激活账号') else: return render(request, 'users/login.html', {'msg': '邮箱或密码有误'}) else: return render(request, 'users/login.html', {'user_login_form': user_login_form})
def post(self, request): login_form = UserLoginForm(request.POST) if login_form.is_valid(): username = request.POST.get('username') password = request.POST.get('password') remember = request.POST.get('remember') typess = request.POST.get('typess') if typess == 'member': is_staff = 0 else: is_staff = 1 user = authenticate(username=username, password=password) print('aaaaa') if user is not None: if user.is_active: login(request, user) print('------------>', user.is_staff, typess) if user.is_staff == True: if typess == 'user': return redirect(reverse('admin')) else: return redirect(reverse('users:index')) elif user.is_staff == False and typess == 'member': return redirect(reverse('users:index')) else: return render(request, 'users/login.html', {'msg': '没有权限'}) else: return render(request, 'users/login.html', {'msg': '用户未激活,请去激活'}) else: return render(request, 'users/login.html', {'msg': '用户名或密码错误'}) else: return render(request, 'users/login.html', {'login_form': login_form})
def view_login(request, login_template, next=''): from users.forms import UserLoginForm if request.method == 'POST': data = post_data(request) next = data.get('next') if not next else next form = UserLoginForm(data) if form.is_valid(): try: userprofile = UserProfile.objects.get(user__email=form.cleaned_data.get('email'), user__is_active=True) except UserProfile.DoesNotExist: from users.messages import USER_LOGIN_FAILURE messages.error(request, USER_LOGIN_FAILURE) return response(request, login_template, {'form': form, 'next': next}) if not userprofile.check_password(form.cleaned_data.get('password')): from users.messages import USER_LOGIN_FAILURE messages.error(request, USER_LOGIN_FAILURE) return response(request, login_template, {'form': form, 'next': next}) from users.messages import USER_LOGIN_SUCCESSFUL messages.success(request, USER_LOGIN_SUCCESSFUL) return _let_user_login(request, userprofile.user, email=form.cleaned_data.get('email'), password=form.cleaned_data.get('password'), next=next) else: form = UserLoginForm() return response(request, login_template, {'form': form, 'next': next})
def login(request): data = UserLoginForm(request.POST) if not data.is_valid(): return HttpResponse('数据不合法') res = data.cleaned_data user = authenticate(**res) if user is None: return HttpResponse('用户名或密码错误') user_login(request,user) url_source = request.META['HTTP_REFERER'] return redirect(url_source)
def post(self, request, *args, **kwargs): data = request.POST form = UserLoginForm(data) if form.is_valid(): # 1. 验证表单成功,获取用户信息 name_mobile = form.cleaned_data['name_mobile'] user = UserModel.objects.filter( Q(username=name_mobile) | Q(mobile=name_mobile)).first() # 修改session中的登录状态 request.session['user_id'] = user.id return redirect('/') else: # 如果表单验证不成功,则可以从form.errors中获取到验证失败的错误信息 return render(request, 'login.html', {'form': form, 'data': data})
def login_view(request): form = UserLoginForm(request.POST or None) next_ = request.GET.get('next') if form.is_valid(): username = request.POST.get('username') password = request.POST.get('password') user = authenticate(username=username.strip(), password=password.strip()) login(request, user) next_post = request.POST.get('next') redirect_path = next_ or next_post or '/' return redirect(redirect_path) context = {'form': form} return render(request, 'users/login.html', context)
def login_view(request): if request.method == 'POST': user_form = UserLoginForm(request.POST) if user_form.is_valid(): user = user_form.login(request) if user: login(request, user) return redirect('main:shop') else: user_form = UserLoginForm() context = { 'user_form': user_form } return render(request, 'users/login.html', context)
def post(self, request): next_ = request.GET.get('next') if request.GET.get( 'next') is not None else reverse('home') if request.user.is_authenticated(): return HttpResponseRedirect(next_) form = UserLoginForm(request.POST) if form.is_valid(): user = form.login(request) if user: login(request, user) return HttpResponseRedirect(next_) return render(request, 'login.html', { 'login_form': form, 'next': next_ })
def login(request): if request.method == 'POST': form = UserLoginForm(request=request, data=request.POST) if form.is_valid(): username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') user = authenticate(username=username, password=password) if user is not None and user.is_staff: auth_login(request, user) return redirect('myadmin:index') else: form.add_error('', '请输入管理员账号') else: form = UserLoginForm() return render(request, 'myadmin/login.html', {'form': form})
def post(self, request, *args, **kwargs): form = UserLoginForm(request.POST) if form.is_valid(): # 使用auth,验证用户名和密码是否正确 user = auth.authenticate(request, username=request.POST.get('username'), password=request.POST.get('password')) if user: # 如果验证用户成功,则获取到user对象,并使用Djnaog自带的auth的login方法实现登录 auth.login(request, user) return redirect('backapp:index') else: # 如果校验用户失败,则跳转到登录页面并提示用户名或者密码错误 return render(request, 'login.html', {'errors': '用户名或者密码错误'}) else: return render(request, 'login.html', {'form': form})
def loguj_user(request): if request.method == 'POST': form = UserLoginForm(request.POST) if form.is_valid(): login = form.cleaned_data['login'] haslo = form.cleaned_data['haslo'] user = authenticate(request, ) if user is not None: login(request, user) messages.success(request, 'Udało się zalogować') return redirect(reverse('user:index')) else: form = UserLoginForm() kontekst = {'form': form} return render(request, 'users/loguj_user.html', kontekst)
def post(self, request): context = self.get_context_data() if USER_FORM_PREFIX in request.POST: user_form = UserRegisterForm(request.POST, prefix=USER_FORM_PREFIX) context.update({'user_form': user_form}) if user_form.is_valid(): register = user_form.save(commit=False) register.save() m = Activation(user=register, key=get_random_string(length=50)) m.save() subject = 'Hello' message = settings.LOCAL_HOST_ADDRESS + reverse('activation', args=[m.key]) sender = settings.EMAIL_HOST_USER to_list = register.email msg = EmailMessage(subject, message, sender, [to_list]) msg.content_subtype = "html" msg.send() if register: messages.success(request, _('Registration Successful. Thank you.')) context.update({ 'user_form': UserRegisterForm(prefix=USER_FORM_PREFIX) }) return super(UserView, self).render_to_response(context) messages.error(request, _('Registration failed. Try again.')) if USER_LOGIN_PREFIX in request.POST: print(request.user.is_authenticated()) user_login = UserLoginForm(request.POST, prefix=USER_LOGIN_PREFIX) context.update({'user_login': user_login}) if user_login.is_valid(): email = user_login.cleaned_data.get("email") password = user_login.cleaned_data.get('password') user = authenticate(email=email, password=password) login(request, user) print(request.user.is_authenticated()) return HttpResponseRedirect('/index/') return super(UserView, self).render_to_response(context)
def loguj(request): if request.method == 'POST': form = UserLoginForm(request.POST) if form.is_valid(): nazwa = form.cleaned_data['login'] haslo = form.cleaned_data['haslo'] user = authenticate(request, username=nazwa, password=haslo) if user is not None: login(request, user) messages.success(request, "Zostałeś zalogowany!") return redirect(reverse('users:index')) else: messages.success(request, "Błędne login lub haslo!") else: form = UserLoginForm() kontekst = {'form': form} return render(request, 'users/loguj.html', kontekst)
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 not check_password(form.cleaned_data['pwd'], user.password): return HttpResponseRedirect(reverse('users:login')) # 添加登录成功的验证 request.session['user_id'] = user.id return HttpResponseRedirect(reverse('home:index')) else: return render(request, 'login.html', {'form': form})
def user_login(request): if request.user.is_authenticated(): nextpage = request.session.get('next', reverse('index')) return HttpResponseRedirect(nextpage) auth_form = UserLoginForm() if request.method == 'POST': nextpage = request.session.get('next', None) if nextpage: del request.session['next'] else: nextpage = reverse('index') auth_form = UserLoginForm(data=request.POST) if auth_form.is_valid(): login(request, auth_form.get_user()) return HttpResponseRedirect(nextpage) else: return render_to_response('login.html', {'auth_form': auth_form}, RequestContext(request)) request.session['next'] = request.GET.get('next') return render_to_response('login.html', {'auth_form': auth_form}, RequestContext(request))
def login(request): if request.user.is_authenticated(): return redirect('users.views.home') if request.method == "POST": login_form = UserLoginForm(request.POST) if login_form.is_valid(): user = authenticate(username = login_form.cleaned_data['username'],password=login_form.cleaned_data['password']) if user is not None: #Login successful auth_login(request,user) return redirect('users.views.home') else: login_error = True return render_to_response('login.html',locals(),context_instance = RequestContext(request)) else: login_error = True return render_to_response('login.html',locals(),context_instance = RequestContext(request)) else: login_form = UserLoginForm() return render_to_response('login.html',locals(),context_instance = RequestContext(request))
def user_login(request): if request.method == 'GET': return render(request, "users/login.html") else: print("++++++++++++++++++++++++++++++++++++++") user_login_form = UserLoginForm(request.POST) if user_login_form.is_valid(): print("LOG+++++++++++++++") username = user_login_form.cleaned_data['username'] password = user_login_form.cleaned_data['password'] user = authenticate(username=username, password=password) if user: login(request, user) return redirect(reverse('index')) else: return render(request, 'users/login.html', {"msg": "用户名或密码错误"}) else: return render(request, 'users/login.html', {'user_login_form': user_login_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(): # 校验用户名和密码,判断返回的对象是否为空,如果不为空,则为user对象 user = auth.authenticate(username=form.cleaned_data['username'], password=form.cleaned_data['password']) if user: # 用户名和密码是正确的,则登录 auth.login(request, user) return HttpResponseRedirect(reverse('user:index')) else: # 密码不正确 return render(request, 'login.html', {'error': '密码错误'}) else: return render(request, 'login.html', {'form': form})
def user_login(request): if request.method == 'GET': return render(request, 'user_login.html') else: loginform = UserLoginForm(request.POST) if loginform.is_valid(): username = loginform.cleaned_data['email'] password = loginform.cleaned_data['password'] user = authenticate(username=username, password=password) if user: userobj = UserProfile.objects.filter(username=username)[0] if userobj.is_start: login(request, user) return redirect('/') else: return HttpResponse('请先去激活账号,在登录') else: return render(request, 'user_login.html', {'msg': '用户名或密码错误'}) else: return render(request, 'user_login.html', {'loginform': loginform})
def post(self, request): form = UserLoginForm(request, data=request.POST) if form.is_valid(): user = authenticate(request, username=form.cleaned_data.get('username'), password=form.cleaned_data.get('password')) if user is None: return render(request, 'survey/login.html', { 'form': form, 'invalid_creds': True }) try: form.confirm_login_allowed(user) except ValidationError: return render(request, 'survey/login.html', { 'form': form, 'invalid_creds': True }) login(request, user) return redirect(reverse('profile'))
def test_login_view(self): c = Client() # making a user for testing form = UserLoginForm( data={ 'username': "******", 'email': "*****@*****.**", 'password1': "test@123", 'password2': "test@123" }) self.assertTrue(form.is_valid()) form.save() #1 username is blank response = c.post('/login/', {'username': "", 'password': "******"}) self.assertIsNotNone(response.context) #2 password is blank response = c.post('/login/', {'username': "******", 'password': ""}) self.assertIsNotNone(response.context) #3 username is wrong response = c.post('/login/', { 'username': "******", 'password': "******" }) self.assertIsNotNone(response.context) #4 passwrod is wrong response = c.post('/login/', { 'username': "******", 'password': "******" }) self.assertIsNotNone(response.context) #5 correct login response = c.post('/login/', { 'username': "******", 'password': "******" }) self.assertEquals(response.url, '/')
def login(request): if request.method == 'POST': next = request.POST.get('next', '/') form = UserLoginForm(request=request, data=request.POST) if form.is_valid(): username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') user = authenticate(username=username, password=password) if user is not None: auth_login(request, user) return redirect(next) else: print(form.errors) else: next = request.GET.get('next', '/') form = UserLoginForm() print('next') return render(request, 'registration/login.html', { 'form': form, 'next': next })
def login(request): ''' 登录 ''' data = {} if request.method == 'GET': form = UserLoginForm() data['form'] = form elif request.method == 'POST': form = UserLoginForm(request.POST) data['form'] = form if form.is_valid(): username = form.cleaned_data['username'] password = form.cleaned_data['password'] user = auth.authenticate(username=username, password=password) if user is not None and user.is_active: auth.login(request, user) user.incr_login_num() log.info("%s login!", user.username) return HttpResponseRedirect("/product/index/") else: data[contenttype.ERROR] = "username or password error!" return render_to_response('user/login.html', data, RequestContext(request))
def userLogin(request): form = UserLoginForm() context = {} context["form"] = form if request.method == "POST": form = UserLoginForm(request.POST) # print("enter the if block") if form.is_valid(): # print("enter the valid function") username = form.cleaned_data.get("username") password = form.cleaned_data.get("password") user = authenticate(request, username=username, password=password) if user: # print("user okay") login(request, user) return render(request, "users/home.html") else: return render(request, "users/login.html", context) else: # print("not okay if blocks") context["form"] = form return render(request, "users/login.html", context)
def login(request): """Checks login details and logs the user in""" if request.user.is_authenticated: return redirect(reverse('index')) if request.method == "POST": login_form = UserLoginForm(request.POST) if login_form.is_valid(): user = auth.authenticate(username=request.POST['username'], password=request.POST['password']) if user: auth.login(user=user, request=request) messages.success(request, "You have logged in successfully!") return redirect(reverse('index')) else: login_form.add_error(None, "Your have entered an incorrect username or password") else: login_form = UserLoginForm() return render(request, 'login.html', {'login_form': login_form})
def login(request, template="users/login.html"): """Displays the login form and handles the login action.""" if request.method == "POST": form = UserLoginForm(data=request.POST) if form.is_valid(): auth_login(request, form.get_user()) if request.session.test_cookie_worked(): request.session.delete_test_cookie() return HttpResponse(simplejson.dumps({'response': '/', 'result': 'success'})) else: response = {} for k,v in form.errors.items(): response[k] = v return HttpResponse(simplejson.dumps({'response': response, 'result': 'error'})) login_form = UserLoginForm(request) register_form = RegisterForm request.session.set_test_cookie() return direct_to_template(request, template, extra_context={'login_form': login_form, 'register_form':register_form})
def user_login(request): if request.method == 'GET': return render(request, 'users/login.html') else: # 获取form登陆表单数据 user_login_form = UserLoginForm(request.POST) if user_login_form.is_valid(): # 验证登陆的邮箱与密码数据 # 验证成功,获取提交的数据 email = user_login_form.cleaned_data['email'] password = user_login_form.cleaned_data['password'] # 验证用户信息,正确返回True user = authenticate(username=email, password=password) # print(user) if user: # 判断用户是否被激活,如果没激活则无法登陆 if user.is_start: # 已激活 login(request, user) # 登陆成功后发一条信息 userMessage = UserMessage() userMessage.message_man = user.id userMessage.message_content = '欢迎登陆...' userMessage.save() # 访问需要登录用户的url,在登陆后接着访问之前的url url = request.COOKIES.get('url', '/') result = redirect(url) result.delete_cookie('url') return result # return redirect(reverse('index')) else: return HttpResponse("你的账号未被激活,请去邮箱激活,否则无法登陆...") else: return render(request, 'users/login.html', {'mgs': '用户名或密码错误'}) else: return render(request, 'users/login.html', {'user_login_form': user_login_form})
def post(self, request, *args, **kwargs): data = request.POST form = UserLoginForm(data) if form.is_valid(): # 1. 验证表单成功,获取用户信息 username = form.cleaned_data['username'] password = form.cleaned_data['password'] user = User.objects.filter(username=username).first() # 如果验证用户名不正确,则返回登录页面 if not user: return redirect('users:login') # 如果验证用户存在,但是密码错误,则返回登录页面 if not check_password(password, user.password): return redirect('users:login') # 如果用户验证成,则修改session中的登录状态 request.session['login_status'] = True # 2. cookie和session的应用 # 2.1 向cookie中保存ticket参数 ticket = get_random_ticket() out_time = datetime.now() + timedelta(days=1) res = HttpResponseRedirect(reverse('home:index')) res.set_cookie('ticket', ticket, expires=out_time) # 2.2 向user_ticket表中存ticket参数,判断如果存在则修改,如果不存在则创建 user_ticket = UserTicket.objects.filter(user=user).first() if user_ticket: # 更新user_ticket表数据 user_ticket.ticket = ticket user_ticket.out_time = out_time user_ticket.save() else: # 创建user_ticket表数据 UserTicket.objects.create(user=user, ticket=ticket, out_time=out_time) return res else: # 如果表单验证不成功,则可以从form.errors中获取到验证失败的错误信息 return render(request, 'web/login.html', {'form': form, 'data': data})
def user_login(request): next = request.GET.get('next', None) context = {} form = UserLoginForm(request.POST or None) if request.method == 'POST': if form.is_valid(): username = form.cleaned_data.get('username') password = form.cleaned_data.get('password') user = authenticate(username=username, password=password) if user is None: messages.error(request, 'Invalid Login Details') return redirect('users:login') else: login(request, user) if next: return redirect(next) return redirect('money:list_account') context['form'] = form return render(request, 'users/login.html', context)
def test_valid_login(self): data = {'email':'*****@*****.**', 'password':'******'} form = UserLoginForm(data) self.assertTrue(form.is_valid()) self.assertFalse(form.errors)