Как скрыть встроенное сообщение об ошибке django AuthenticationForm?

Я новичок в django и пытаюсь скрыть сообщение об ошибке, на котором есть пуля (см. скриншот ниже), встроенным атрибутом error_message при использовании класса AuthenticationForm на логине, потому что я использовал form.errors в шаблонах и хотел сохранить сообщение об ошибке наверху. Я пытался использовать css для скрытия, но когда я загружаю страницу входа, это не работает. Есть ли способ скрыть или, возможно, отключить его? Я вижу только варианты настройки error_message в документации

Вот мой код.

login.htmlenter image description here

{% extends 'blog/base.html' %} 
{% load crispy_forms_tags %}
{% block content %}  
<!-- Custom Error Message -->
{% if  form.errors %}
    {% for _, error in form.errors.items %}
        <div class="alert alert-danger text-center">
            {{ error|striptags }}
        </div>    
    {% endfor %}
{% endif %}
  <div class="content-section">
      <form method="POST">
          {% csrf_token %}
          <fieldset class="form-group">
              <legend class="border-bottom mb-4">Log in</legend>
              {{ form | crispy }}
          </fieldset>
          <div class="form-group">
              <button class="btn btn-outline-info" type="submit">Login</button>
              <small class="text-muted ml-2">
                  <a href="{% url 'password_reset' %}">Forgot Password?</a>
              </small>
          </div>
      </form>
      <div class="border-top pt-3">
          <small class="text-muted">
              Need An Account? <a href="{% url 'register' %}" class="ml-2">Sign Up</a>
          </small>
      </div>
  </div>
{% endblock content %}

forms.py

class LogInAuthForm(AuthenticationForm):
    username = forms.CharField(label='', widget=forms.TextInput(attrs={'style': 'margin:1rem 0 4rem', 'placeholder': 'Username'}))
    password = forms.CharField(label='', widget=forms.PasswordInput(attrs={'class': 'mb-4', 'placeholder': 'Password'}))

    error_messages = {
        'invalid_login': _("Invalid login. Note that both " "fields may be case-sensitive."),
        'inactive': _("This account is inactive."),
    }

views.py

class CustomLoginView(LoginView):
    authentication_form = LogInAuthForm


{{form|crispy}} это создает встроенную шаблонную форму. Попробуйте проинспектировать элемент на этих тегах ввода, и вы увидите класс или id формы-ошибки, которая создает эти сообщения об ошибках. Итак, попробуйте следующее (оформляйте по своему усмотрению).

{% load crispy_forms_tags %}
{% block content %}  
<!-- Custom Error Message -->
{% if  form.errors %}
    {% for _, error in form.errors.items %}
        <div class="alert alert-danger text-center">
            {{ error|striptags }}
        </div>    
    {% endfor %}
{% endif %}
  <div class="content-section">
      <form method="POST">
          {% csrf_token %}
          <fieldset class="form-group">
              <legend class="border-bottom mb-4">Log in</legend>
              <input class="form-class" name="username" placeholder="Username">
             <input class="form-class" name="password" placeholder="Password">
          </fieldset>
          <div class="form-group">
              <button class="btn btn-outline-info" type="submit">Login</button>
              <small class="text-muted ml-2">
                  <a href="{% url 'password_reset' %}">Forgot Password?</a>
              </small>
          </div>
      </form>
      <div class="border-top pt-3">
          <small class="text-muted">
              Need An Account? <a href="{% url 'register' %}" class="ml-2">Sign Up</a>
          </small>
      </div>
  </div>
{% endblock content %}```

Если вы не хотите отображать форму по умолчанию с ошибками, вы можете отобразить ее вручную.

Замените:

<fieldset class="form-group">
    <legend class="border-bottom mb-4">Log in</legend>
    {{ form | crispy }}
</fieldset>

с чем-то вроде

<fieldset class="form-group">
    <legend class="border-bottom mb-4">Log in</legend>
    {{ form.username|as_crispy_field }}
    {{ form.password|as_crispy_field }}
</fieldset>
Вернуться на верх