我写了一个简单的登录功能,如下所示。 Django开发版本是1.9.8,python版本是3.5。
The login function
def login(req):
if req.method == "POST":
username = req.POST.get("username")
password = req.POST.get("psw")
kwargs = {'username': username, 'password': password}
user_ = auth.authenticate(**kwargs)
if user_ is not None and user_.is_active:
auth.login(req,user_)
return render(req, "home.html", {"status": "Logged In Successess"})
else:
return render(req, "home.html", {"status": "Log In Failed. Check!"})
else:
return render(req, "login.html")
else:
return render(req, "login.html")
settings.py
AUTHENTICATION_BACKENDS=[
'django.contrib.auth.backends.ModelBackend',
]
AUTH_USER_MODEL = "shop.customer"
在插入大量断点以观察身份验证行为后,我发现错误发生在Django源https://github.com/django/django/blob/stable/1.9.x/django/contrib/auth/backends.py
的指令(第18~19行)中。
if user.check_password(password):
return user
总是给我一个anonymous user
。所以我转而创建一个超级用户
然后再试一次。同样的结果仍在继续。有人请给我提示和建议,谢谢!!!
更新
这是我的shop.customer
型号:
from django.contrib.auth.models import (
BaseUserManager, AbstractUser
)
class customer(AbstractUser):
def __str__(self):
return self.username
USERNAME_FIELD = 'username'