Better way to re-order Django form errors? -


is there better, more "django-istic", way put django form error messages in particular order technique shown below? have form ten fields in it. if user doesn't fill out required field or if enter invalid data, iterate through form's error fields i've put in custom error list displaying red "x" , error message(s) @ top , red "x" next invalid field(s) below:

{# template.html #} <form method="post" action=".">     {% csrf_token %}     {# non-field errors removed clarity #}      {# iterate through error messages in custom ordered error list #}     {% error_message in ordered_error_list %}         <img src='/static/img/red_x.png' alt='error'></img>         &nbsp;{{ error_message }}<br>     {% endfor %}     <br>      {% 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 clarity #}     <p><input type="submit" value="continue" /></p> </form> 

here's view , helper function:

# views.py def create_member_profile(request):     if request.method == "post":         form = creatememberprofileform(request.post)         if form.is_valid():             # process form data , redirect next page...             return httpresponseredirect(reverse('review-member-profile'))         # before re-post form , display errors, i'll put 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     error in form['field_1'].errors:         errors_in_order.append(error)     error in form['field_2'].errors:         errors_in_order.append(error)     error in form['field_3'].errors:         errors_in_order.append(error)     # ...     error in form['field_10'].errors:         errors_in_order.append(error)     return errors_in_order 

the reason seems necessary form.errors dictionary , python dictionaries, definition, unordered. however, said, want error messages @ top displayed in same order form fields refer to. couldn't find form.errors attributes allow me re-order form errors. don't "for error in form[]" blocks seem required if want strip html tag beginning of each error. note i'm not checking errors putting each "for" loop inside "if form['field'].errors" block because omitting doesn't make difference , want code run fast possible.

is there better way this? thanks!

you this. same idea s using (two loops).

<!-- near top --> <tr> {% field in form %}     <td>{{ field.label_tag }}: {{ field.errors }}</td> {% endfor %} </tr>  <!-- form --> {% field in form %} <tr>     <td>{{ field.label_tag }}:</td>     <td>         {{ field }}         {{ field.help_text }}     </td> </tr> {% endfor %} 

format wish, think kind of gives idea. can customize error messages in form clean:

from django import forms  yourform(forms.form):     ....     def field_clean(self):  # or use clean(self) multiple fields         ....         raise forms.validationerror(u'your custom error')     .... 

more on form field validation django docs.


Comments

Popular posts from this blog

java - activate/deactivate sonar maven plugin by profile? -

python - TypeError: can only concatenate tuple (not "float") to tuple -

java - What is the difference between String. and String.this. ? -