0

I want to remove the circled bit from being displayed. How would I be able to do this?

{% if form.errors %}
  <div class="alert alert-warning alert-dismissible fade show"
       role="alert">
      <p class="m-0">{{ form.errors }}</p>
    <button type="button"
            class="btn-close"
            data-bs-dismiss="alert"
            aria-label="Close">
    </button>
  </div>
{% endif %}

This always displays

  • property where the error is found
    • error message
    • error message ...

I want to remove the property where the error is found. How would I do this?

2 Answers 2

2

In Django 4.0 or high

                {%  if form.errors  %}
                    {% for error in form.errors.as_data.values %}
                        {% for data in error.0 %}
                            {{ data }}
                        {% endfor %}
                    {% endfor %}
                {% endif %}
Sign up to request clarification or add additional context in comments.

Comments

1

Django Form has two error handlers:

  • Form.errors are field errors. By default, a dict of key "field name" : value "field errors"

  • Form.non_field_errors are general errors that have no relation to a field name, mainly errors from the Form.clean(..) method.

So you are trying to do an anti-pattern and a distraction to the user experience. Therefore you can best achieve a better experience by mapping each field to its value without the need to render the field name in the error.

Form custom rendering will give you full access to the Form API, and allow you to display the error in the way you like without the default render, something like the following looping over form's fields:

{% for field in form %}
    <div class="fieldWrapper">
        {{ field.errors }}
        {{ field.label_tag }} {{ field }}
        {% if field.help_text %}
        <p class="help">{{ field.help_text|safe }}</p>
        {% endif %}
    </div>
{% endfor %}

By doing the above, you will also need to render the non_field errors, and the complete HTML tags will be as follows:

{{ form.non_field_errors }}
{% for field in form %}
    <div class="fieldWrapper">
        {{ field.errors }}
        {{ field.label_tag }} {{ field }}
        {% if field.help_text %}
        <p class="help">{{ field.help_text|safe }}</p>
        {% endif %}
    </div>
{% endfor %}

Django also offers manual render for each field something like the following:

{{ form.non_field_errors }}
<div class="fieldWrapper">
    {{ form.subject.errors }}
    <label for="{{ form.subject.id_for_label }}">Email subject:</label>
    {{ form.subject }}
</div>
<div class="fieldWrapper">
    {{ form.message.errors }}
    <label for="{{ form.message.id_for_label }}">Your message:</label>
    {{ form.message }}
</div>
<div class="fieldWrapper">
    {{ form.sender.errors }}
    <label for="{{ form.sender.id_for_label }}">Your email address:</label>
    {{ form.sender }}
</div>
<div class="fieldWrapper">
    {{ form.cc_myself.errors }}
    <label for="{{ form.cc_myself.id_for_label }}">CC yourself?</label>
    {{ form.cc_myself }}
</div>

Choose the best fit for your needs.


There is still a hacky thing you can do which is not a best practice and will lead to conflicts between Field validation and non-field validation as follows Getting a list of errors in a Django form:

{% if form.errors %}
    {% for field in form %}
        {% for error in field.errors %}
            <div class="alert alert-danger">
                <strong>{{ error|escape }}</strong>
            </div>
        {% endfor %}
    {% endfor %}
{% endif %}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.