Djangouser.is活跃

jjjwad0x  于 2023-02-17  发布在  Go
关注(0)|答案(1)|浏览(87)

为什么当我停用用户在Django管理网站在我的类在后方法
如果需求用户不是None,需求是否首先返回负数?
也许如果用户否定真Django不看他在用户表?

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())
mnemlml8

mnemlml81#

authenticate函数中,django在settings.py中的AUTHENTICATION_BACKENDS上调用authenticate
ModelBackend是Django提供的一个默认的身份验证后端,如果你正在使用它,它会检查用户是否处于活动状态。

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

相关问题