Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Django output form errors as table rows in {{ form.as_table }}

I really find the form output shortcuts such as as_table really handy. However, displaying errors while using those methods seems a little counterintuitive to me. When I use the as_table format, I would like my field specific errors to be displayed in accordance to table formatting. I can manually piece my forms together like so:

<table>
{% for error in form.non_field_errors %}
<tr><td>{{ error }}</td></tr>
{% endfor %}
{% endif %}

{% if form.username.errors %}
{% for error in form.username.errors %}
<tr><td>{{ error }}</td></tr>
{% endfor %}
{% endif %}
<tr><th><label for="id_username">Name:</label></th><td>{{ form.username }}</td></td>

{% if form.password.errors %}
{% for error in form.password.errors %}
<tr><td>{{ error }}</td></tr>
{% endfor %}
{% endif %}
<tr><th><label for="id_password">Password:</label>/th><td>{{ form.password }}</td></td>

But what I want to know is that if there a simpler way to do this? Maybe something I missed in the docs? Or perhaps a different method any of you employ?

like image 834
chandsie Avatar asked Apr 15 '11 21:04

chandsie


People also ask

What method can you use to check if form data has changed when using a form instance?

Use the has_changed() method on your Form when you need to check if the form data has been changed from the initial data. has_changed() will be True if the data from request.

What is form Is_valid () in Django?

The is_valid() method is used to perform validation for each field of the form, it is defined in Django Form class. It returns True if data is valid and place all data into a cleaned_data attribute.

What is form AS_P in Django?

{{ form.as_p }} – Render Django Forms as paragraph. {{ form.as_ul }} – Render Django Forms as list.

What is form cleaned_data in Django?

cleaned_data returns a dictionary of validated form input fields and their values, where string primary keys are returned as objects. form. data returns a dictionary of un-validated form input fields and their values in string format (i.e. not objects).


1 Answers

How errors are displayed and customizing the error list format show what the default error field output is and how to customize it.

I have been using a reusable template in my projects recently, which has been working well for me.

table_form.html:

<table>
{% for error in form.non_field_errors %}
    <tr><td>{{ error }}</td></tr>
{% endfor %}

{% for field in form %}
    {% for error in form.username.errors %}
    <tr><td>{{ error }}</td></tr>
    {% endfor %}
    <tr><th>{{ field.label_tag }}:</th><td>{{ field }}</td></td>
{% endfor %}
</table>

template.html:

<form>
    {% include 'table_form.html' %}
</form>

multiple forms work too, e.g. view with context including form1 and form2:

template.html:

<form>
    {% include 'table_form.html with form=form1 %}
</form>

<form>
    {% include 'table_form.html with form=form2 %}
</form>

edit:

Here is the as_table method as defined in the BaseForm class:

210     def as_table(self):
211         "Returns this form rendered as HTML <tr>s -- excluding the <table></table>."
212         return self._html_output(
213             normal_row = u'<tr%(html_class_attr)s><th>%(label)s</th><td>%(errors)s%(field)s%(help_text)s</td></tr>',
214             error_row = u'<tr><td colspan="2">%s</td></tr>',
215             row_ender = u'</td></tr>',
216             help_text_html = u'<br /><span class="helptext">%s</span>',
217             errors_on_separate_row = False)

overriding this method in your form will allow you to change the rendering when you use {{ form.as_table }}

like image 121
dting Avatar answered Oct 27 '22 00:10

dting