[Answered ]-Django not displaying error_messages in forms

1👍

Did you try looping through form.errors in template?

{% if form.errors %}
    {% for field in form %}
        {% for error in field.errors %}
          {{ error }}  
        {% endfor %}
    {% endfor %}
    {% for error in form.non_field_errors %}
        {{ error }}
    {% endfor %}
{% endif %}

0👍

When you are rendering fields manually you would have to use error tags.

{{ form.name_of_field.errors }}

        <form method="post">
           {% csrf_token %}
          
          <div class="form-row">
            <div class="form-group col-md-6">
              {{ form.name|as_crispy_field }}
              {{ form.name.errors}}

            </div>
            <div class="form-group col-md-6">
              {{ form.email|as_crispy_field }}
              {{ form.email.errors}}

            </div>
          </div>
          <div class="form-group">
            {{ form.subject|as_crispy_field }}
              {{ form.subject.errors}}

            {{ form.message|as_crispy_field }}
              {{ form.message.errors}}

          </div>
          <div class="text-center"><button type="submit" class="btn btn-primary ">Send Message</button></div>
        </form>

https://docs.djangoproject.com/en/dev/topics/forms/#rendering-fields-manually

Leave a comment