django 认证Users

认证Users:

authenticate(**credentials)[source]

请使用authenticate(),认证一组给定的用户名和密码。

它接收关键字参数形式的凭证,使用默认配置参数是username和password,

如果密码能够匹配给定的用户名,它将返回一个User 对象。

如果密码无效,authenticate()返回None。例子:




def main(req):
    # Do something for anonymous users.
    from django.contrib.auth import authenticate, login
    username=req.POST['username']
    password = req.POST['password']
    user = authenticate(username=username, password=password)
    print '1111111111111111111111111111111'
    print user
    print type(user)
    print '1111111111111111111111111111111'




成功登陆:


1111111111111111111111111111111
admin
<class 'django.contrib.auth.models.User'>
1111111111111111111111111111111



密码错误:

1111111111111111111111111111111
None
<type 'NoneType'>
1111111111111111111111111111111



正常登陆:
if user.is_active:
            print '--------user.is_active----------------'
            print user.is_active
            print '--------user.is_active----------------'
			
			
--------user.is_active----------------
True
--------user.is_active----------------
原文地址:https://www.cnblogs.com/hzcya1995/p/13348990.html