Is_valid() function of the authentication form is not working when the credential is wrong

I have extended the built in authentication form to use bootstrap naming LoginForm. If the user gives wrong credential, I want to show message that "Wrong Credential".But the is_valid() function only work when the credential is (username,password) is exactly the same. Let's say i put username right but wrong password, then the is_valid() function doesn't work.As a result i cant show the message that the password is wrong. But I have created another form by subclassing forms.Form. in that case the is_valid() faunction works for wrong credential too and shows the message for wrong username and password.

this is my form.

class LoginForm(AuthenticationForm):
    username = UsernameField(
        widget = forms.TextInput(attrs={'autocomplete':'off','class':'form-control','placeholder':'Enter your email'})
        )
    password = forms.CharField(
        label=_('Password'),
        strip=False,
        widget=forms.PasswordInput(attrs={'autocomplete':'off','class':'form-control','placeholder':'Enter your password'})
        )

this is the views.py

def signin(request):
    if request.method == 'POST':
        form = LoginForm(data=request.POST)
        if form.is_valid():
            print('1')
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            user = authenticate(request,username=username,password=password)
            print('2')
            if user is not None:
                login(request,user)
                print('3')

                return redirect('registration')
            else:
                print('4')
                messages.warning(request,'Wrong Credentials!')
        else:  # Use form.errors for displaying errors
            print(form.errors)
    else:
        print('get')
        form = LoginForm()
    return render(request,'account/login.html',{'form':form})

since this form does not work. for testing purpose I've created another form. and this works fine and shows the error message of "Wrong credentials" if any of the field does not match the username and password.

class LoginForm(forms.Form):
    def __init__(self,*args,**kwargs):
        super().__init__(*args,**kwargs)

    username = forms.CharField(max_length=150,widget=forms.TextInput(attrs={"autofocus": True,'class':'form-control','placeholder':'Enter your Email',}))

    password = forms.CharField(max_length=150, widget=forms.PasswordInput(attrs={"autocomplete": "current-password",'class':'form-control','placeholder':'Enter your password',}))
Back to Top