Укажите свою собственную процедуру аутентификации, тогда вы можете проверить (или не проверить) все, что вам нравится. Мы делаем это так, что если они терпят неудачу при обычном имени пользователя, мы также можем впустить их по электронной почте / паролю (хотя это не то, что я показываю ниже).
в settings.py:
AUTHENTICATION_BACKENDS = (
'django.contrib.auth.backends.ModelBackend',
'userprofile.my_authenticate.MyLoginBackend', # if they fail the normal test
)
в userprofile / my_authenticate.py:
from django.contrib.auth.backends import ModelBackend
from django.contrib.auth.models import User
class MyLoginBackend(ModelBackend):
"""Return User record if username + (some test) is valid.
Return None if no match.
"""
def authenticate(self, username=None, password=None, request=None):
try:
user = User.objects.get(username=username)
# plus any other test of User/UserProfile, etc.
return user # indicates success
except User.DoesNotExist:
return None
# authenticate
# class MyLoginBackend