handle server-side form errors

This commit is contained in:
checktheroads 2021-03-14 01:06:51 -07:00
parent d0cb7d843d
commit 59d8c0b321
2 changed files with 36 additions and 0 deletions

View File

@ -0,0 +1,26 @@
{% load form_helpers %}
{% if form.errors or form.non_field_errors %}
<div class="alert alert-danger mt-3" role="alert">
<h4 class="alert-heading">Errors</h4>
<hr />
<div class="ps-2">
{% if form.errors %}
{% for field_name, errors in form.errors.items %}
{% with field=form|getfield:field_name %}
<strong>{{ field.label }}</strong>
<ul>
{% for error in errors %}
<li>{{ error }}</li>
{% endfor %}
</ul>
{% endwith %}
{% endfor %}
{% endif %}
{% if form.non_field_errors %}
<hr />
{{ form.non_field_errors }}
{% endif %}
</div>
</div>
{% endif %}

View File

@ -54,3 +54,13 @@ def widget_type(field):
return field.field.widget.__class__.__name__.lower()
else:
return None
@register.inclusion_tag('utilities/render_errors.html')
def render_errors(form):
"""
Render form errors, if they exist.
"""
return {
"form": form
}