前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python测试开发django-74.auth认证之is_active

python测试开发django-74.auth认证之is_active

作者头像
上海-悠悠
发布2020-07-01 16:10:16
6040
发布2020-07-01 16:10:16
举报

前言

在 django 的 User 表里面有个 is_active 字段可以判断用户是否是激活状态。 使用 authenticate 校验登录的时候 is_active 是不生效的。

authenticate 登录

create_user 创建新用户的时候 is_active 默认是1,也就是True

代码语言:javascript
复制
D:\code202003\MyDjango>python manage.py shell
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.contrib.auth.models import User
>>> from django.contrib.auth import authenticate
>>> user=User.objects.create_user(username="test",password="test")
>>> user
<User: test>
>>> user.is_active
True

当修改用户的 is_active 状态,改成 False 时

代码语言:javascript
复制
>>> a=authenticate(username="test",password="test")
>>> a.is_active=False
>>> a.save()
>>> a.is_active
False

再次用 authenticate 校验登录状态

代码语言:javascript
复制
>>> from django.contrib.auth.models import User
>>> from django.contrib.auth import authenticate
>>> a=authenticate(username="test",password="test")
>>> a

此时账号密码验证不通过,这样就跟输错密码是一样的了,无法知道用户is_active状态

不检测用户的活跃状态

django的默认配置

会检测用户是否是活跃状态(is_active),不活跃则返回None(默认配置)

AUTHENTICATION_BACKENDS = [‘django.contrib.auth.backends.ModelBackend’]

需在 settings.py 文件里加上下面的配置

代码语言:javascript
复制
# 不会检测用户的活跃状态
AUTHENTICATION_BACKENDS = ['django.contrib.auth.backends.AllowAllUsersModelBackend']

is_active

加上配置后,重新打开shell

代码语言:javascript
复制
D:\code202003\MyDjango>python manage.py shell
Python 3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 08:06:12) [MSC v.1900 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> from django.contrib.auth.models import User
>>> from django.contrib.auth import authenticate
>>> a=authenticate(username="test",password="test")
>>> a
<User: test>
>>> a.is_active
False

这样 is_active 就会生效了!

本文参与 腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2020-06-28,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 从零开始学自动化测试 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • authenticate 登录
  • 不检测用户的活跃状态
  • 会检测用户是否是活跃状态(is_active),不活跃则返回None(默认配置)
  • is_active
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档