Django:我如何使用auth_user表的is_active?

Bur*_*ing 2 python authentication django django-models python-2.7

我不知道什么是使用param的好时机.

djangoproject描述如下:

布尔.指定是否应将此用户帐户视为活动帐户.我们建议您将此标志设置为False而不是删除帐户; 这样,如果您的应用程序有任何外键给用户,外键不会中断.

这不一定控制用户是否可以登录.验证后端不需要检查is_active标志,默认后端不需要.如果要拒绝基于is_active为False的登录,则由您自己在登录视图或自定义身份验证后端中进行检查.但是,login()视图使用的AuthenticationForm(默认值)会执行此检查,权限检查方法(如has_perm()和Django admin中的身份验证)也会执行此检查.对于非活动用户,所有这些函数/方法都将返回False.

readthedocs描述如下:

Authorization for inactive users

非活动用户是经过身份验证但其属性is_active设置为False的用户.但这并不意味着他们无权做任何事情.例如,他们可以激活他们的帐户.

权限系统中对匿名用户的支持允许匿名用户具有执行某些操作的权限,而非活动身份验证用户则无权执行某些操作.

不要忘记在您自己的后端权限方法中测试用户的is_active属性.

任何人都可以给出一些让我知道param需要注意或如何使用它的例子.

jat*_*tin 10

from django.contrib.auth import authenticate
user = authenticate(username='john', password='secret')
if user is not None:  #to check whether user is available or not?
    # the password verified for the user
    if user.is_active:   
        print("User is valid, active and authenticated")
    else:
        print("The password is valid, but the account has been disabled!")
else:
    # the authentication system was unable to verify the username and password
    print("The username and password were incorrect.")
Run Code Online (Sandbox Code Playgroud)

这将有助于您了解django身份验证.


Ken*_*ley 7

非活动用户是指其 is_active 字段设置为 False 的用户。

\n

从 django 版本 1.10 开始:ModelBackend(默认身份验证后端)和 RemoteUserBackend 身份验证后端禁止这些非活动用户进行身份验证。因此,如果您使用这些后端,则不需要使用以下样式:

\n
#authentication has been successful now...\nif user.is_active:\n    login(request,user)\n    #redirect to success page\nelse:\n    #return disabled account error message\n
Run Code Online (Sandbox Code Playgroud)\n

如果自定义用户模型\xe2\x80\x99t 没有 is_active 字段,则所有用户都将被允许进行身份验证。\n在版本 1.10 之前的 ModelBackend 允许非活动用户进行身份验证 - 这首先在允许用户进行身份验证的情况下很有用,然后再进行身份验证您允许用户激活他们的帐户(仅在他们成功通过身份验证后)。

\n

请注意,@login_required 装饰器不会检查用户的 is_active 标志。@要求登录

\n

检查 AUTHENTICATION_BACKENDS 以查看您正在使用哪些后端。\n请参阅https://docs.djangoproject.com/en/1.10/topics/auth/customizing/

\n