Вам необходим пользовательский сервер аутентификации doc
Примерно так:
class MyAuthenticationBackend:
def authenticate(self, email_username=None, password=None):
if "@" in email_username:
try:
user = User.objects.get(email=email_username)
except User.DoesNotExist:
return None
else:
try:
user = User.objects.get(username=email_username)
except User.DoesNotExist:
return None
if getattr(user, 'is_active', False) and user.check_password(password):
return user
return None
def get_user(self, user_id):
try:
return User.objects.get(pk=user_id)
except User.DoesNotExist:
return None
Затем в виде формы:
from django.contrib.auth import authenticate
from django.contrib.auth import login
from django.shortcuts import redirect
class LoginView(FormView):
template_name = 'my-auth/login.html'
form_class = LoginForm
def form_valid(self, form):
data = form.cleaned_data
user = authenticate(email_username=data['email_username'], password=data['password'])
if user is None:
form.add_error(None, _('Wrong email/username'))
return self.form_invalid(form)
login(self.request, user)
return redirect('somewhere')