python - How to handle form.non_field_errors and form.errors? -



python - How to handle form.non_field_errors and form.errors? -

i need display non_field_errors , field errors in different styles. did below

{% if form.non_field_errors %} <div class="alert alert-danger" role="alert"> {{ form.non_field_errors }} </div> {% endif %} {% if form.errors %} <div class="alert alert-info" role="alert"> {% field in form %} {% if field.errors %} {{ field.errors| striptags }} {% endif %} {% endfor %} </div> {% endif %}

but when there non field error without field errors, both sections displayed. how can display non field error section if there no field errors?

your if status {% if form.errors %} triggered when there form errors, including non field errors.

maybe can convert sec block loop , place if status within:

{% field in form %} {% if field.errors %} <div class="alert alert-info" role="alert"> {{ field.errors }} </div> {% endif %} {{ field }} {% endfor %}

this simple example. can extend , adapt according needs.

edit:

even improve solution utilize django-crispy-forms:

http://django-crispy-forms.readthedocs.org/en/latest/

don't reinvent wheel!

edit: reply first comment below

if want display field errors in single div, whatever reason might need that, write:

{% if form.errors %} {# first block #} {% if form.non_field_errors %} {# logic #} {% endif %} {# sec block #} <div class="alert alert-info" role="alert"> {% field in form %} {{ field.errors }} {# logic #} {% endfor %} </div> {% endif %}

so if there field errors, non field errors block won't show up.

python django django-templates

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -