zoukankan      html  css  js  c++  java
  • python测试开发django-74.auth认证之is_active

    前言

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

    authenticate 登录

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

    D:code202003MyDjango>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 时

    >>> a=authenticate(username="test",password="test")
    >>> a.is_active=False
    >>> a.save()
    >>> a.is_active
    False
    

    再次用 authenticate 校验登录状态

    >>> 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 文件里加上下面的配置

    # 不会检测用户的活跃状态
    AUTHENTICATION_BACKENDS = ['django.contrib.auth.backends.AllowAllUsersModelBackend']
    

    is_active

    加上配置后,重新打开shell

    D:code202003MyDjango>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 就会生效了!

  • 相关阅读:
    20款最优秀的JavaScript编辑器
    清空windows系统网络配置
    如何禁止Chrome浏览器隐藏URL的WWW前缀
    EDK2开发环境搭建
    edk2中子目录介绍
    INTEL_BIOS 编译—for-ATOM_E3800
    英特尔vPro博锐技术激活
    gitea configure
    mpeg1、mpeg2和mpeg4标准对比分析和总结
    内置缓存
  • 原文地址:https://www.cnblogs.com/yoyoketang/p/13192138.html
Copyright © 2011-2022 走看看