django用户验证模块,不用密码登录

1.django用户验证模块(/django/contrib/auth/__init__.py)

from django.contrib.auth import authenticate, login
username = request.POST['username']
password = request.POST['password']
user = authenticate(username=username, password=password)
if user is not None:
    login(request, user)

 

2.有时候,我们想指定用户登录但是我们不知道该用户的密码,默认的 authenticate 方法必须有 password 传递进去,其执行结果是给 user 对象赋了一个 backend 属性,为此我写了一个方法来模拟这个功能,实现不知道密码的情况下登录指定用户

 

def login_user(request, user):
    user.backend = 'django.contrib.auth.backends.ModelBackend'
    from django.contrib.auth import login
    login(request, user)

 

posted @ 2017-08-02 16:13  嫁给幸福  阅读(1841)  评论(0编辑  收藏  举报