我可以在视图或settings.py中编写什么,使用哪个身份验证系统?
这是我的views.py
@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
AUTHENTICATION_BACKENDS = ['accounts.backends.TeacherBackend','accounts.backends.StudentBackend']backends.py
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请告诉我如何编写代码来决定调用哪个后端??
请不要读堆栈溢出迫使我写更多的描述,但我无法思考,所以我写这个虚拟文本作为描述。保持冷静。提前感谢
发布于 2019-07-13 15:23:57
您必须在登录表单中区分登录类型。一种方法是在登录表单中使用下拉框。为此,必须在登录表单中添加一个自定义字段:
在forms.py中
from django.contrib.auth.forms import AuthenticationForm
class CustomLoginForm(AuthenticationForm):
# Choose login type
login_type = forms.ChoiceField(
choices=(
('student', 'Student'),
('teacher', 'Teacher')
)
)在views.py中
class CustomLoginView(LoginView):
authentication_form = CustomLoginForm在urls.py中
url(r'^login/', CustomLoginView.as_view(redirect_authenticated_user=True), name='login')在backends.py中
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中
AUTHENTICATION_BACKENDS = [
'accounts.backends.TeacherBackend',
'accounts.backends.StudentBackend'
# ... Any other following backends
]在模板/注册/login.html中
# ... Include with other Login fields
{{ form.login_type }} Django按照给定的顺序检查每个后端,如果任何后端引发PermissionDenied异常,则执行将停止。因此,每种登录类型都将仅由相应的后端进行验证。
https://stackoverflow.com/questions/39212323
复制相似问题