我如何在我的 django 项目(扩展的 AbstractBaseUser 模型)的 settings.py 文件中将我的身份验证后端设置为默认

how do i set my authentication backend as default in settings.py file of my django project(Extended AbstractBaseUser model)

我为 Auth 后端编写了以下 class 并将其放在应用程序目录内的文件“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")

但我得到以下回溯:

ImportError at /login Module "events" does not define a "authentication" attribute/class

我是菜鸟,我很确定我做错了什么,我只是不明白它是什么。

谁能告诉我正确的做法吗?

您还需要使用 class 名称导入它,因此:

AUTHENTICATION_BACKENDS = [
    <b>'events.authentication.authBackend'</b>
]

在你的authBackend中,你也犯了一些错误。首先是身份验证后端 needs to implement a number of functions [Django-doc]:

The user model and its manager will delegate permission lookup functions (get_user_permissions(), get_group_permissions(), get_all_permissions(), has_perm(), has_module_perms(), and with_perm()) to any authentication backend that implements these functions.

因此您也需要实现这些功能。因此,从 BaseBackend [Django-doc] or perhaps even better from the ModelBackend 继承可能更好,因为它已经实现了逻辑,因此您只需要重写某些函数,让它与您的新模型一起工作。

你的参数名也打错了:是password,不是passwprd:

from django.contrib.auth.backends import <b>BaseBackend</b>
from events.models import User

class authBackend(<b>BaseBackend</b>):
    def authenticate(self, request, username<b>, password</b>):
        try:
            user = User.objects.get(rollNo=username)
            success = user.check_password(<b>password</b>)
            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

Note: According to the PEP-8 Style guide [pep-0008], class names are written in PerlCase starting with an uppercase, so you might want to consider renaming authBackend to AuthBackend.