-1

Why when i desactive user on Django admin site in my class in post method
requirement return negative first if requirement user is not None ?

Probably if user desative true Django don`t look him in user table ?

class LoginView(View):
    template_name = 'login.html'

    def get(self, request):
        form = LoginForm()
        return render(request, self.template_name, locals())

    def post(self, request):
        form = LoginForm(request.POST)
        if form.is_valid():
            username = form.cleaned_data.get('username')
            password = form.cleaned_data.get('password')
            user = authenticate(username=username, password=password)
            if user is not None:
                if user.is_active:
                    login(request, user)
                    return redirect('home')
                else:
                    alert = messages.error(request, 'Twoje konto zostało zablokowane!')
                    return render(request, self.template_name, locals())
            else:
                alert = messages.error(request, 'Błędna nazwa użytkownika!')
                return render(request, self.template_name, locals())

1 Answers1

1

In authenticate function, django call authenticate on your AUTHENTICATION_BACKENDS in settings.py.

ModelBackend is a default authentication backend that has been provided by Django, and if you are using it, it checks if user is acive or not. It's sth like this:

def user_can_authenticate(self, user):
    """
    Reject users with is_active=False. Custom user models that don't have
    that attribute are allowed.
    """
    is_active = getattr(user, 'is_active', None)
    return is_active or is_active is None
Amin
  • 2,605
  • 2
  • 7
  • 15