I would like to log in pages with both username and email, I wrote a CustomBackend view to auth no matter how I changed, only username works, I use "email" to log in pages, always got error ofUnable to log in with provided credentials, partial code of views.py as below:
from django.contrib.auth import get_user_model
MyUser = get_user_model()
class CustomBackend(object):
def authenticate(self, username=None, password=None, **kwargs):
try:
user = MyUser.objects.get(Q(username=username)|Q(email=username))
if user.check_password(password):
return user
except MyUser.DoesNotExist:
MyUser().set_password(password)
partial code of settings.py as below:
AUTHENTICATION_BACKENDS = (
'general.views.CustomBackend',
'django.contrib.auth.backends.ModelBackend'
)
Any ideas will be highly appreciated.