1

Here I am trying to display some messages if the username and password is correct but if the is_active field is false but it is not displaying the message with this code.It is displaying me the else part even if the username and password are correct of that inactive user.How can I do it ?

I think after making is_active field to false it sets the User to None

views.py

form = LoginForm()
    if request.method == 'POST':
        form = LoginForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data['username']
            password = form.cleaned_data['password']
            remember_me = form.cleaned_data['remember_me']
            user = authenticate(request, username=username, password=password)

            if user and not user.is_active:
                messages.info(request,'Sorry your account is deactivated now.')
                return redirect('/login/')

            elif user and user.is_active:
                login(request, user)
                if not remember_me:
                    request.session.set_expiry(0)
                else:
                    request.session.set_expiry(settings.SESSION_COOKIE_AGE)
             else:
                 messages.error(request, 'Invalid login credentials. Try Again.')
                 return redirect('/login/')
Hello
  • 312
  • 1
  • 5
  • 14

1 Answers1

1

This answer helped me.

I added this in my settings.py file and it worked.

AUTHENTICATION_BACKENDS = ['django.contrib.auth.backends.AllowAllUsersModelBackend']
Hello
  • 312
  • 1
  • 5
  • 14