Getting a list of errors in a Django form

前端 未结 4 1404
失恋的感觉
失恋的感觉 2020-12-13 13:39

I\'m trying to create a form in Django. That works and all, but I want all the errors to be at the top of the form, not next to each field that has the error. I tried loopin

4条回答
  •  半阙折子戏
    2020-12-13 14:00

    You can use this code:

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

    this add https://docs.djangoproject.com/en/3.0/ref/forms/api/#django.forms.Form.non_field_error

提交回复
热议问题