I have a form in django and I wanna show errors before each field.

The problem is when I use form.field_name.errors or form.errors.field_name, it happens for one of the fields that error does not show up, just for one of them, here's the template code:

<table class="">
                <form action="" method="POST" enctype="multipart/form-data">

                    {% csrf_token %}

                    <div>
                        <div>{{ form.errors.competitor_name }}</div>
                        <br/>
                        <div>name:</div>
                        <div>{{ form.competitor_name }}</div>
                    </div>
                    <div>
                        <div>{{ form.errors.notional_code }}</div>
                        <br/>
                        <div>code:</div>
                        <div>{{ form.national_code }}</div>
                    </div>
                    <div>
                        <table id="filesContainer">
                            <tbody>
                            {% for form_ in formset.forms %}
                                <tr id="{{ form_.prefix }}-row">
                                    <td>{{ form_.file.label }}:</td>
                                    <td>{{ form_.file }}</td>
                                    <td></td>
                                </tr>
                            {% endfor %}
                            </tbody>
                        </table>
                        <p>
                            {{ formset.management_form }}
                        </p>
                    </div>
                    <div class="arsh-signup-row">
                        <input type="submit" value="SignUp" />
                    </div>
                </form>
            </table>

I have problem in showing errors of national_code field.

I have used break points and I absolutely realized that I'm adding errors in the right way and everything about form is alright, it seems something is wrong with the template and I don't know what's this.

The interesting part is, when I wanna show up this field's error in some other part of the page, it's ok, everything is done, but it doesn't show in that specific part, if I use this code:

<div>
                        <div>{{ form.errors.notional_code }}</div>
                        <br/>
                        <div>name:</div>
                        <div>{{ form.competitor_name }}</div>
                    </div>
                    <div>
                        <div>{{ form.errors.notional_code }}</div>
                        <br/>
                        <div>code:</div>
                        <div>{{ form.national_code }}</div>
                    </div>

I can see what I want. It's really funny at first, but now it's confusing for me.

Any advice would be appreciated.

有帮助吗?

解决方案 2

the problem would be solved with this code lines:

{% for field in form %} 
    <div class="row"> 
        <div class="error">{{ field.errors }}</div> 
        <br/> 
        <div class="lable">{{ field.label }}</div> 
        <div class="field">{{ field }}</div> 
    </div> 
{% endfor %}

;-)

其他提示

You get errors in field_name.errors for each field. form.errors gives any error for entire form and not specific to a field.

Refer below sample from django docs here

<form action="/contact/" method="post">
{% for field in form %}
    <div class="fieldWrapper">
        {{ field.errors }}
        {{ field.label_tag }}: {{ field }}
    </div>
{% endfor %}
<p><input type="submit" value="Send message" /></p>

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top