原因1:使用django自带的authenticate()进行登录认证的时候,post传的数据为username:XXXX,password:XXXX,需要与数据库中保存的字段名称一致,使用默认的AbstractUser作为模型类时,默认的字段名子为username,如果post传回的json对象中不是username时候,就会有以下错误:
原因2:
路由的正则匹配没有匹配到数据
mismissing attribute django.template.base.VariableDoesNotExist: Failed lookup for key [name] in '<RegexURLResolver <RegexURLPattern list> (admin:admin) admin/>
源码:
class ModelBackend(object):
def authenticate(self, request, username=None, password=None, **kwargs):
if username is None:
username = kwargs.get(UserModel.USERNAME_FIELD)
try:
user = UserModel._default_manager.get_by_natural_key(username)
except UserModel.DoesNotExist:
# Run the default password hasher once to reduce the timing
# difference between an existing and a non-existing user (#20760).
UserModel().set_password(password)
else:
if user.check_password(password) and self.user_can_authenticate(user):
return user```