Django- how to use built-in login view with email instead of username?
django, django-authentication, django-login, django-registration
Solution
By default `django.contrib.auth.urls` will create a log in page from this pattern
(r'^login/$', 'django.contrib.auth.views.login'),
You need to avoid/override this url then create a new view to handle a new type of login.
e.g.
create a new login url in your urls.py
(r'^emaillogin/$', 'email_login_view'),
create view to support login with email in views.py
# get default authenticate backend
from django.contrib.auth import authenticate, login
from django.contrib.auth.models import User
# create a function to resolve email to username
def get_user(email):
try:
return User.objects.get(email=email.lower())
except User.DoesNotExist:
return None
# create a view that authenticate user with email
def email_login_view(request):
email = request.POST['email']
password = request.POST['password']
username = get_user(email)
user = authenticate(username=username, password=password)
if user is not None:
if user.is_active:
login(request, user)
# Redirect to a success page.
else:
# Return a 'disabled account' error message
else:
# Return an 'invalid login' error message.
Ref : https://docs.djangoproject.com/en/1.4/topics/auth/#django.contrib.auth.login
Problem
I am using built-in login in my app. There is some custom backends or packages to handle this. but many of them are not what i am looking. i made email unique via django-registration while registering. now all i want is to ask email in login page instead of username. but if i use some custom backends like django email as username it crashes when using with django-registration. i dont want to change all authentication backend , i just want to change login page. in the rest of site , i am gonna use username. p.e in my custom admin page when i write: ``` welcome {{user}} ``` it must render username. not e-mail. i need to find the way out from this. i am stuck. thank you.