一、用户验证功能
Django自带用户验证及登录功能,引入模块为:
from django.contrib.auth import authenticate
其中方法authenticate()的接收参数为:
def authenticate(request=None, **credentials):
传入参数:
user = authenticate(username=login_user, password=login_password)
authenticate方法自动在数据库中匹配、验证,但是不能实现邮箱登录的验证,需要对该方法重写,导入模块:
from django.contrib.auth.backends import ModelBackend
创建重写类:
class ChongxieAuthenticate(ModelBackend):
def authenticate(self, username=None, password=None, **kwargs):
try:
user = UserProfile.objects.get(Q(username=username) | Q(email=username))
if user.check_password(password):
return user
else:
return None