django - 如何将 django 中的管理页面与自定义身份验证后端连接?

标签 django django-admin django-authentication

我在 django 项目中使用了自定义身份验证后端来连接用户,我的问题是我无法再使用管理界面进行连接。 这是我的自定义身份验证文件: auth_backends.py:

from django.conf import settings
from django.contrib.auth.backends import ModelBackend
from django.core.exceptions import ImproperlyConfigured
from django.db.models import get_model


class StudentModelBackend(ModelBackend):
def authenticate(self, username=None, password=None):
    try:
        user = self.user_class_s.objects.get(username=username)
        if user.check_password(password):
            return user
    except self.user_class_s.DoesNotExist:
        return None

def get_user(self, user_id):
    try:
        return self.user_class_s.objects.get(pk=user_id)
    except self.user_class_s.DoesNotExist:
        return None

@property
def user_class_s(self):
    if not hasattr(self, '_user_class'):
        self._user_class = get_model(*settings.CUSTOM_USER_MODEL.split('.', 2))
        if not self._user_class:
            raise ImproperlyConfigured('Could not get student model')
    return self._user_class


class ProfessorModelBackend(ModelBackend):
def authenticate(self, username=None, password=None):
    try:
        user = self.user_class_p.objects.get(username=username)
        if user.check_password(password):
            return user
    except self.user_class_p.DoesNotExist:
        return None

def get_user(self, user_id):
    try:
        return self.user_class_p.objects.get(pk=user_id)
    except self.user_class_p.DoesNotExist:
        return None

@property
def user_class_p(self):
    if not hasattr(self, '_user_class'):
        self._user_class = get_model(*settings.CUSTOM_USER_MODEL_P.split('.', 2))
        if not self._user_class:
            raise ImproperlyConfigured('Could not get student model')
    return self._user_class

settings.py:

AUTHENTICATION_BACKENDS = (
'authentification.auth_backends.StudentModelBackend',
'authentification.auth_backends.ProfessorModelBackend',
)

CUSTOM_USER_MODEL = 'forum.Student'
CUSTOM_USER_MODEL_P = 'forum.Professor'

我尝试了这个解决方案: 从 django.contrib.auth.decorators 导入 login_required

admin.autodiscover()
admin.site.login = login_required(admin.site.login)

但它会将我重定向到用户身份验证界面而不是管理界面。

有人可以帮我吗?

最佳答案

我找到了解决方案, 如果用户不是学生或教授,我只需告诉 django 尝试使用原始后端进行连接。 只需在 settings.py 中添加 'django.contrib.auth.backends.ModelBackend'

设置.py:

AUTHENTICATION_BACKENDS = (
'authentification.auth_backends.StudentModelBackend',
'authentification.auth_backends.ProfessorModelBackend',
'django.contrib.auth.backends.ModelBackend',
)

关于django - 如何将 django 中的管理页面与自定义身份验证后端连接?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29753102/

相关文章:

python - Django 表单

django - django中比萨的阿拉伯语问题

python - Django 不提供静态文件,出现 404 错误

Django:将组重命名为角色

django - Django 内联管理中的模型 "help"文本

django - 自动生成的字段 'user_ptr' 与声明的同名字段冲突

Django:登录后激活用户存储的语言

android - 获取 token 身份验证 View 时出现 Django REST HTTP 400 错误

python - Django 虚拟环境布局

python - Django 模板标签返回 True 或 False