【发布时间】:2020-02-08 12:27:13
【问题描述】:
我正在开发一个 Django 项目,在该项目中我定义了一个自定义用户模型,我需要为其编写自定义身份验证方法,按照我编写的文档,如下所示但是我在调用它时遇到问题views.py 请通过查看以下代码来帮助我
我已将我的自定义后端定义如下
我的自定义身份验证后端
from django.contrib.auth.backends import BaseBackend
from .models import User
from IntellerMatrix.CommonUtilities.constants import Constants
class AuthenticationBackend(BaseBackend):
"""
Authentication Backend
:To manage the authentication process of user
"""
def authenticate(self, email=None, password=None):
user = User.objects.get(email=email)
if user is not None and user.check_password(password):
if user.is_active == Constants.YES:
return user
else:
return "User is not activated"
else:
return None
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
settings.py
AUTHENTICATION_BACKENDS = ['Modules.users.authentication.AuthenticationBackend',
'django.contrib.auth.backends.ModelBackend', ]
Views.py
def login(request):
email = 'ialihaider75@gmail.com'
password = 'ali'
user = # how to call here that custom authentication backend's authenticate method
if user is None:
return HttpResponse("<p>Not Valid</p>")
else:
return HttpResponse(user)
【问题讨论】:
标签: python django django-authentication custom-authentication