开发者

django accessing errors

I have the following:

{% if formDetails.errors %}
{% for key, value in formDetails.errors.items %}
{% for error in value %}
    <div class="ui-widget" id="id-error">
        <div class="ui-state-error ui-corner-all" style="padding: 0 .7em;">
            <p><span class="ui-icon ui-icon-alert" style="float: left; margin-right: .3em;"></span>
            <strong>{{ key }}:</strong> {{ error }}</p>
        </div>
    <开发者_Python百科/div>
{% endfor %}
{% endfor %}

But unfortunately key gives me the name of the model field (the one that's lowercase with underscores). How can I get the nice looking name like field.label_tag?


The form.errors template variable is a list not a dictionary, so you should access it as follows:

{% for error in form.errors %}
    <li>{{ error }}</li>
{% endfor %}

In addition it contains form wide errors raised by the clean() method on the Form object you are dealing with. Validation errors raised by the individual fields, can be accessed like this:

{% for field in form %}
    {% for error in field.errors %}
        {{ error }}
    {% endfor %}
    {{ field.label_tag }}
{% endfor %}

Have a read of this part of the Django docs, it seems like you don't have a full understanding of what you have when accessing a form.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜