Getting a list of errors in a Django form

前端 未结 4 1411
失恋的感觉
失恋的感觉 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:07

    If you want something simple with a condition take this way :

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

    If you want more info and see the name and the error of the field, do this:

    {% if form.errors %}
      
      {% for key,value in form.errors.items %}
    • {{ key|escape }} : {{ value|escape }}
    • {% endfor %}
    {% endif %}

    If you want to understant form.errors is a big dictionary.

提交回复
热议问题