Better way to re-order Django form errors?
Is there a better, more "Django-istic", way to put Django form error
messages in a particular order than the technique shown below? I have a
form with ten fields in it. If the user doesn't fill out a required field
or if they enter invalid data, I iterate through the form's error fields
which I've put in a custom error list by displaying a red "x" and the
error message(s) at the top and a red "x" next to the invalid field(s)
below:
{# template.html #}
<form method="post" action=".">
{% csrf_token %}
{# Non-field errors removed for clarity #}
{# Iterate through error messages in custom ordered error list #}
{% for error_message in ordered_error_list %}
<img src='/static/img/red_x.png' alt='Error'></img>
{{ error_message }}<br>
{% endfor %}
<br>
{% for field in form.visible_fields %}
<div>
{{ field.label_tag }}<br />
{{ field }}
{% if field.errors %}
<img src='/static/img/red_x.png' alt='Error'></img>
{% endif %}
</div>
{% endfor %}
{# Hidden fields removed for clarity #}
<p><input type="submit" value="Continue" /></p>
</form>
Here's my view and helper function:
# views.py
def create_member_profile(request):
if request.method == "POST":
form = CreateMemberProfileForm(request.POST)
if form.is_valid():
# Process form data and redirect to next page...
return HttpResponseRedirect(reverse('review-member-profile'))
# Before I re-POST the form and display the errors, I'll put the
errors in order
else:
ordered_error_list = put_member_profile_errors_in_order(form)
else:
form = CreateMemberProfileForm()
return render_to_response(template, locals(),
context_instance=RequestContext(request))
def put_member_profile_errors_in_order(form):
errors_in_order = [] # List
for error in form['field_1'].errors:
errors_in_order.append(error)
for error in form['field_2'].errors:
errors_in_order.append(error)
for error in form['field_3'].errors:
errors_in_order.append(error)
# ...
for error in form['field_10'].errors:
errors_in_order.append(error)
return errors_in_order
The reason this all seems necessary is that form.errors is a dictionary
and Python dictionaries, by definition, are unordered. However, as I said,
I want any error messages at the top to be displayed in the same order as
the form fields they refer to. I couldn't find any form.errors attributes
that would allow me to re-order the form errors. I don't like the "for
error in form[]" blocks but they seem to be required if I want to strip
the HTML tag from the beginning of each error. Also note that I'm not
checking for errors by putting each "for" loop inside an "if
form['field'].errors" block because omitting this doesn't make a
difference and I want this code to run as fast as possible.
Is there a better way to do this? Thanks!
No comments:
Post a Comment