'Django : How i can logged in by email instead of username

I want to make the email be set in the frontend of the django application.

  1. I have go and create this class to make the authentification based on the email

    class EmailBackend(ModelBackend):
        def authenticate(self, request, username=None, password=None, **kwargs):
            UserModel = get_user_model()
            try:
                user = UserModel.objects.get(email=username)
            except UserModel.DoesNotExist:
                return None
            else:
                if 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
    
  2. Then I have go and define the path of this class in the settings.py

  3. Everything is good without error and I logged in by typing the email. But in the frontend, the label is still "Username". How can I modify it?

enter image description here

Here it's the Html code form login page:

<form method="POST">
            {% csrf_token %} <!--this protect our form against certeain attacks ,added security django rquires-->
            <fieldset class="form-group">
              {{ form|crispy}} 
            </fieldset>
            <div class="form-group">
                <button class="btn btn-outline-info" type="submit">Login</button>
                <small class="test-muted ml-2">
                   <a class="ml-2" href="{% url 'password_reset' %}">Forgot Password?</a>
                </small> 
            </div>
            <!--- put div for a link if he is already have account--->
            <div class="border-top pt-3">
                <small class="test-muted">Need an account? <a class="ml-2" href="{% url 'register' %}">Sign Up</a></small> 
                <!--it a bootstrap -->
            </div>
        </form>

Edited: This is the view code:

def login_view(request):
    
    context = {}
    user = request.user  
    destination = get_redirect_if_exists(request)

    if request.method == "POST":
        form = LoginForm(request.POST)
        if form.is_valid():
            email = form.cleaned_data["email"]
            password = form.cleaned_data["password"]
            user = authenticate(request, email=email, password=password)
            if user == None:
                attempt = request.session.get("attempt") or 0
                request.session['attempt'] = attempt + 1
                return render(request, 'pages/login.html')
            else:
                login(request, user)
                destination = get_redirect_if_exists(request)
                if destination:
                    return redirect(destination)
                return redirect("home")
    return render(request, 'pages/login.html')

and this is the login form code:

class LoginForm(UserCreationForm):
    email = forms.CharField(label='email')
    password = forms.CharField(label='password')


Solution 1:[1]

Probably you should change form

urlpatterns = [
   path('login/', LoginView.as_view(authentication_form=YourForm), name='login'),
]

class YourForm(AuthenticationForm):
    username = forms.CharField(widget=TextInput(attrs={'class':'validate','placeholder': 'Username or Email'}))
    password = forms.CharField(widget=PasswordInput(attrs={'placeholder':'Password'})

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 igor Smirnov