Django Form Rendering Position

When I show the error form with {{f.as_p}}, the ul error list always comes first, and then the label and input field. For example:

<ul class="errorlist">
<li>This field is required.</li>
</ul>
<p>
<label for="id_content">Content:</label>
<textarea id="id_content" class="required error" name="content" cols="80" rows="10"/>
</p>

I know you can use

{% for field in f %}
<p>{{ field.label_tag }}: {{ field }}</p>
{{ field.errors }}
{% endfor %}

To change the default ul position in the error list after the label and field, but I want to use f.as_p, f.as_table or {{f}} directly because it is simple and easy, especially when I have to show a lot of forms.

So the question is, is there a way to make errorlist ul display after the default part of the field?

+5
source share
4 answers

I always use a loop that is pretty general, but very customizable:

{% for field in form %}
<tr>
    <td>{{ field.label_tag }}:</td>
    <td>
        {{ field.errors }}
        {{ field }}
        {% if field.help_text %}
        <img class="tooltip" title="{{ field.help_text }}" src="{{ MEDIA_URL }}images/icon_help.png">
        {% endif %}
    </td>
</tr>
{% endfor %}

Between ofc table tags :)

+6
source

, , - Form ( ModelForm) , html. .

+3

Form.as_p() - Form. , Form - as_p . , Django.

+1

jquery appendTo, ul django fields. html , html jjdo jQuery, .

$(document).ready(function() {
    $("ul.errorlist").each(function(index){
        $(this).appendTo($(this).parent());
    });
});
0

All Articles