1from django.contrib.auth import authenticate, login
2
3def my_view(request):
4 username = request.POST['username']
5 password = request.POST['password']
6 user = authenticate(request, username=username, password=password)
7 if user is not None:
8 login(request, user)
9 # Redirect to a success page.
10 ...
11 else:
12 # Return an 'invalid login' error message.
13 ...
14
1from django.contrib.auth import get_user_model
2from django.contrib.auth.backends import ModelBackend
3
4class EmailBackend(ModelBackend):
5 def authenticate(self, request, username=None, password=None, **kwargs):
6 UserModel = get_user_model()
7 try:
8 user = UserModel.objects.get(email=username)
9 except UserModel.DoesNotExist:
10 return None
11 else:
12 if user.check_password(password):
13 return user
14 return None