首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >我有两个django身份验证后端。我如何在我的特定网页上设置使用哪一个的条件?

我有两个django身份验证后端。我如何在我的特定网页上设置使用哪一个的条件?
EN

Stack Overflow用户
提问于 2016-08-30 01:57:14
回答 1查看 415关注 0票数 1

我可以在视图或settings.py中编写什么,使用哪个身份验证系统?

这是我的views.py

代码语言:javascript
复制
@require_POST
def login_view(request):
    filled_form = LoginForm(request.POST)
    if filled_form.is_valid():
        user = filled_form.get_user()
        login(request,user)
        context = { 'user': user }
        return render(request,'home.html',context)
    else:
        return HttpResponse("Does not Exist")

settings.py

代码语言:javascript
复制
AUTHENTICATION_BACKENDS =   ['accounts.backends.TeacherBackend','accounts.backends.StudentBackend']

backends.py

代码语言:javascript
复制
class StudentBackend(object):
def authenticate(self, username=None, password=None):
    print("AT STUDENT BACK")
    try:
        student = Student.objects.get(student_id=username)
    except Student.DoesNotExist:
        return None
    if student.check_password(password):
        return student
    else:
        return None


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


class TeacherBackend(object):
print("AT TEACHER BACK")
   def authenticate(self, username=None, password=None):
      try:
        teacher = Teacher.objects.get(email=username)
      except Teacher.DoesNotExist:
        return None
      if teacher.check_password(password):
        return teacher
      else:
        return None


   def get_user(self, teacher_id):
     try:
        return Teacher.objects.get(pk=teacher_id)
     except UsTeacherer.DoesNotExist:
        return None

请告诉我如何编写代码来决定调用哪个后端??

请不要读堆栈溢出迫使我写更多的描述,但我无法思考,所以我写这个虚拟文本作为描述。保持冷静。提前感谢

EN

回答 1

Stack Overflow用户

发布于 2019-07-13 15:23:57

您必须在登录表单中区分登录类型。一种方法是在登录表单中使用下拉框。为此,必须在登录表单中添加一个自定义字段:

在forms.py中

代码语言:javascript
复制
from django.contrib.auth.forms import AuthenticationForm

class CustomLoginForm(AuthenticationForm):
    # Choose login type
    login_type = forms.ChoiceField(
        choices=(
            ('student', 'Student'),
            ('teacher', 'Teacher')
        )
    )

在views.py中

代码语言:javascript
复制
class CustomLoginView(LoginView):
    authentication_form = CustomLoginForm

在urls.py中

代码语言:javascript
复制
url(r'^login/', CustomLoginView.as_view(redirect_authenticated_user=True), name='login')

在backends.py中

代码语言:javascript
复制
from django.core.exceptions import PermissionDenied

class StudentBackend:
    def authenticate(self, username=None, password=None):
        print("AT STUDENT BACK")
        if self.request.POST.login_type != 'student':
            return None
        try:
            student = Student.objects.get(student_id=username)
        except Student.DoesNotExist:
            raise PermissionDenied("No such student exists") # Stop executing other backends
        if student.check_password(password):
            return student
        else:
            raise PermissionDenied("Wront credentials for student")


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

class TeacherBackend:
   def authenticate(self, username=None, password=None):
      print("AT TEACHER BACK")
      if self.request.POST.login_type != 'teacher':
        return None
      try:
        teacher = Teacher.objects.get(email=username)
      except Teacher.DoesNotExist:
        raise PermissionDenied("No such teacher exists")
      if teacher.check_password(password):
        return teacher
      else:
        raise PermissionDenied("Wront credentials for teacher")


   def get_user(self, teacher_id):
     try:
        return Teacher.objects.get(pk=teacher_id)
     except UsTeacherer.DoesNotExist:
        return None  

在settings.py中

代码语言:javascript
复制
AUTHENTICATION_BACKENDS =   [
    'accounts.backends.TeacherBackend',
    'accounts.backends.StudentBackend'
    # ... Any other following backends
    ]

在模板/注册/login.html中

代码语言:javascript
复制
# ... Include with other Login fields
{{ form.login_type }}   

Django按照给定的顺序检查每个后端,如果任何后端引发PermissionDenied异常,则执行将停止。因此,每种登录类型都将仅由相应的后端进行验证。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39212323

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档