I have created a custom authenticate method because instead of username I want to log in through email for that I have written backends.py

backends.py

```
from django.contrib.auth import get_user_model 
from django.core.exceptions import ValidationError

UserModel = get_user_model()

class EmailBackend(object):
    def authenticate(username=None,password=None):
        try:
            user = UserModel.objects.get(email=username)
            if user.check_password(password):
                return user
            else:
                return None
        except UserModel.DoesNotExist:
            raise ValidationError('Invalid Credentials')

    def get_user(self,user_id):
        try:
            return UserModel.objects.get(pk=user_id)
        except UserModel.DoesNotExist:
            return None

```

After authenticating i want user to be logged in.For that i am using login method as below:

login(request,user,backend='django.contrib.auth.backends.ModelBackend')

views.py

```
from django.shortcuts import render,redirect
from users.forms import RegisterForm,LoginForm
from users.backends import EmailBackend as em
from django.contrib import messages
from django.contrib.auth.decorators import login_required
from django.contrib.auth import login

def register(request):
    if request.method=='POST':
        form = RegisterForm(request.POST)
        if form.is_valid():
            form.save()
            print('Successfully Created')
    else:
        form = RegisterForm()

    return render(request,'users/register.html',{'form':form})

def user_login(request):
    if request.method == 'POST':
        form = LoginForm(request.POST)
        uname = request.POST['username']
        pwd = request.POST['password']
        user = em.authenticate(username=uname,password=pwd)
        if user is not None:
            if user.is_active==True:
                k=login(request,user,backend='django.contrib.auth.backends.ModelBackend')
                print(user,k)
                print('Successfull login')
        else:
            print('Unsuccessfull login')

    else:
        form = LoginForm()    
    return render(request,'users/login.html',{'form':form})

```

In my console, I am trying to get the value returned by the login function but that is returning None .

k=login(request,user,backend='django.contrib.auth.backends.ModelBackend')
print(user,k)

**Output in my console:**

```
[16/Mar/2021 16:02:34] "POST /register/ HTTP/1.1" 200 881
[16/Mar/2021 16:02:52] "GET /login/ HTTP/1.1" 200 705
testuser@gmail.com None
Successfull login
[16/Mar/2021 16:02:58] "POST /login/ HTTP/1.1" 302 0
[16/Mar/2021 16:02:58] "GET /home HTTP/1.1" 200 237**

```

Print method is giving the correct value of user but for k it is giving None. That means the login function is not working and that means login authentication is not working properly.

How can I do login once authenticate method is returned, user? In which way login can be successful.

My code:

settings.py

```
AUTHENTICATION_BACKENDS = ['django.contrib.auth.backends.ModelBackend',
'users.backends.EmailBackend']

```

models.py

```
from django.db import models
from django.contrib.auth.models import AbstractBaseUser,AbstractUser
from django.utils.translation import gettext_lazy as _

class CustomUser(AbstractBaseUser):
    email = models.EmailField(_('email address'), unique=True)
    last_login = models.DateTimeField(verbose_name='last login', auto_now=True)
    is_superuser = models.BooleanField(default=False)
    is_active = models.BooleanField(default=True)
    is_staff = models.BooleanField(default=False)
    date_joined = models.DateTimeField(verbose_name='date joined', auto_now_add=True)
    USERNAME_FIELD='email'

```

forms.py

```
from django.contrib.auth import get_user_model
from django.contrib.auth.forms import UserCreationForm, AuthenticationForm
from django import forms
from users.models import CustomUser

class RegisterForm(UserCreationForm):
    class Meta():
        model = get_user_model()
        fields = ('email', 'password1', 'password2')

class LoginForm(AuthenticationForm):
    class Meta():
        model = get_user_model()

```