Как отобразить ошибку при использовании is_active для Login

Я установил для пользователя user.is_active значение false, чтобы он не мог войти в систему.

user.is_active=False
user.save()

Я хотел бы переопределить раздел входа, чтобы показать, что учетная запись была отключена. В настоящее время он показывает отключенные учетные записи.

Please enter a correct username and password. Note that both fields may be case-sensitive.

Я использую логин auth

path('accounts/', include('django.contrib.auth.urls')),

с помощью простого шаблона

{% extends 'base.html' %}

{% block title %}Login{% endblock %}

{% block content %}

  <h2>Log In</h2>
  <form method="POST" action="."enctype="multipart/form-data">
    {% csrf_token %}
    {{ form.as_p }}
    <button type="submit">Log In</button>
    <button><a href="{% url 'signup' %}">Sign up</a></button>
  </form>
  
{% endblock %}

Я видел нечто подобное, где они переопределяют clean и вызывают эту функцию.

def confirm_login_allowed(self, user):
        if not user.is_active:
            raise forms.ValidationError(
                "This account has been disabled",
                code='inactive',
            )
from django.contrib import messages
from django.contrib.auth.forms import AuthenticationForm
def login(request):
    if request.method == 'POST':
        form = AuthenticationForm(request.POST)
        username = request.POST['username']
        password = request.POST['password']
        user = authenticate(username=username, password=password)
        if user:
            if user.is_active:
                auth_login(request, user)
                return redirect('home')
            else:
                messages.error(request,'User blocked')
                return redirect('login')
        else:
            messages.error(request,'username or password not correct')
            return redirect('login')

    else:
        form = AuthenticationForm()
    return render(request, 'registration/login.html',{'form':form})

Просто нужно было проверить is_active, затем отправить messages.error после переопределения login.

Вернуться на верх