我为 Auth Backend 编写了以下类,并将其放在 app 目录内的文件“authentication.py”中:
from events.models import User
class authBackend():
def authenticate(self, request, username, passwprd):
try:
user = User.objects.get(rollNo=username)
success = user.check_password(password)
if success:
return user
except User.DoesNotExist:
pass
return None
def get_user(self, uid):
try:
return User.objects.get(pk=Uid)
except:
return None
然后我将它添加(或者至少我认为我做了)它到 settings.py:
AUTHENTICATION_BACKENDS = [
'events.authentication'
]
这就是我登录用户的方式:
def login_view(request):
if request.method == "POST":
# Attempt to sign user in
rollno = request.POST["rollno"]
password = request.POST["password"]
user = authenticate(request, username=rollno, password=password)
# Check if authentication successful
if user is not None:
login(request, user)
return HttpResponseRedirect(reverse("events:index"))
else:
return render(request, "events/login.html", {
"message": "Invalid roll number and/or password."
})
else:
return render(request, "events/login.html")
但我得到以下追溯:
/login 模块“事件”处的 ImportError 未定义“身份验证”属性/类
我是一个菜鸟,我很确定我做错了什么,我只是不明白它是什么。
有人可以告诉我怎么做吗?