Error:Render form errors with the label rather than field name

0 votes

I would like to list all form errors together using {{ form.errors }} in the template. This produces a list of form fields and nested lists of the errors for each field. However, the literal name of the field is used. The generated html with an error in a particular field might look like this.

<ul class="errorlist">
    <li>
        target_date_mdcy
        <ul class="errorlist">
            <li>This field is required.</li>
        </ul>
    </li>
</ul>

I would like use the errorlist feature, as it's nice and easy. However, I want to use the label ("Target Date", say) rather than the field name. Actually, I can't think of a case in which you would want the field name displaying for the user of a webpage. Is there way to use the rendered error list with the field label?

Apr 29 in Python by kartik
• 11,890 points
33 views

1 answer to this question.

0 votes

Hello @kartik,

There is a simple way to use the label:

{% if form.errors %}
    <ul class="user-msg error">
    {% for field in form %}
        {% for error in field.errors %}
            <li>
              {% if field != '__all__' %}
                <strong>{{ field.label }}:</strong>
              {% endif %}
              {{ error }}
            </li>
        {% endfor %}
    {% endfor %}
    </ul>
{% endif %}

hope this works!!

Thank You!!

answered Apr 29 by Niroj
• 23,950 points

Related Questions In Python

0 votes
1 answer
0 votes
0 answers

I am trying to install PyBase64 on my python 3.8. But I end up with the following error:

$ pip install pybase ERROR: Could not find ...READ MORE

Mar 24 in Python by Nishant
• 210 points
194 views
0 votes
0 answers

I am trying to install os-win on my python 3.8. But I end up with the following error:

ERROR: Command errored out with exit status ...READ MORE

Mar 24 in Python by Nishant
• 210 points
300 views
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 6, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 963 views
0 votes
0 answers
0 votes
1 answer

How can I get the domain name of my site within a Django template?

Hello kartik, The variation of the context processor ...READ MORE

answered Apr 23 in Python by Niroj
• 23,950 points
152 views
0 votes
1 answer

Errors in fields not displaying on Django admin form?

Hiii, Here is how you can see what ...READ MORE

answered Apr 29 in Python by Niroj
• 23,950 points
31 views