如何使Django表单错误不在UL中?

时间:2022-01-13 07:46:02

The errors in my Django form are rendering in a UL as per the docs...

我的Django表单中的错误以UL的形式呈现……

Django

Django

{{ form.non_field_errors }}

HTML

HTML

<ul class="errorlist">
  <li>Sender is required.</li>
</ul>

How can I render the errors so they appear not in a UL, but in a paragraph tag for each fields relevant error? So ideally...

如何显示错误,使它们不出现在UL中,而是出现在针对每个字段的段落标记中?所以理想的…

<ul>
  <li>
    <label>...</label>
    <input>...</input>
    <p>Error message...</p>
  </li>
</ul>

EDIT:

编辑:

I should have used this code in my example for clarity...

我本应该在我的示例中使用这段代码以使其清晰……

{{ form.fieldname.errors }}

2 个解决方案

#1


89  

You can display your error as the following in your template:

您可以在模板中显示以下错误:

<p>{{ form.fieldname.errors.as_text }}</p>

#2


12  

It obviously can't render within the context of the field because these are "non-field errors" as the attribute name implies. The only way to fix this is to add the error in the right place when validating. For example, doing the following results in non-field errors:

它显然不能在字段的上下文中呈现,因为这些是属性名所暗示的“非字段错误”。解决这个问题的唯一方法是在验证时将错误添加到正确的位置。例如,做以下操作会导致非字段错误:

class MyModelForm(forms.ModelForm):
    class Meta:
        model = MyModel

    def clean(self):
        somefield = self.cleaned_data.get('somefield')
        if not somefield:
            raise forms.ValidationError('Some field is blank')

However, you can do the following to make that error still show on the right field:

但是,您可以做以下操作,使该错误仍然显示在正确的字段中:

class MyModelForm(forms.ModelForm):
    class Meta:
        model = MyModel

    def clean(self):
        somefield = self.cleaned_data.get('somefield')
        if not somefield:
            if not self._errors.has_key('somefield'):
                from django.forms.util import ErrorList
                self._errors['somefield'] = ErrorList()
            self._errors['somefield'].append('Some field is blank')

UPDATE:

更新:

From the Django docs:

Django的文档:

Each named form-field can be output to the template using {{ form.name_of_field }}, which will produce the HTML needed to display the form widget. Using {{ form.name_of_field.errors }} displays a list of form errors, rendered as an unordered list. This might look like:

每个命名的表单字段都可以使用{{{{{{form.name_of_field}}输出到模板,这将生成显示表单小部件所需的HTML。使用{{{{{{form.name_of_field.errors}显示一个表单错误列表,以无序列表的形式呈现。这样子:

<ul class="errorlist">
    <li>Sender is required.</li>
</ul>

The list has a CSS class of errorlist to allow you to style its appearance. If you wish to further customize the display of errors you can do so by looping over them (emphasis mine):

该列表具有errorlist的CSS类,允许您对其外观进行样式化。如果您希望进一步自定义错误的显示,可以通过循环显示它们(重点是我的):

{% if form.subject.errors %}
    <ol>
    {% for error in form.subject.errors %}
        <li><strong>{{ error|escape }}</strong></li>
    {% endfor %}
    </ol>
{% endif %}

#1


89  

You can display your error as the following in your template:

您可以在模板中显示以下错误:

<p>{{ form.fieldname.errors.as_text }}</p>

#2


12  

It obviously can't render within the context of the field because these are "non-field errors" as the attribute name implies. The only way to fix this is to add the error in the right place when validating. For example, doing the following results in non-field errors:

它显然不能在字段的上下文中呈现,因为这些是属性名所暗示的“非字段错误”。解决这个问题的唯一方法是在验证时将错误添加到正确的位置。例如,做以下操作会导致非字段错误:

class MyModelForm(forms.ModelForm):
    class Meta:
        model = MyModel

    def clean(self):
        somefield = self.cleaned_data.get('somefield')
        if not somefield:
            raise forms.ValidationError('Some field is blank')

However, you can do the following to make that error still show on the right field:

但是,您可以做以下操作,使该错误仍然显示在正确的字段中:

class MyModelForm(forms.ModelForm):
    class Meta:
        model = MyModel

    def clean(self):
        somefield = self.cleaned_data.get('somefield')
        if not somefield:
            if not self._errors.has_key('somefield'):
                from django.forms.util import ErrorList
                self._errors['somefield'] = ErrorList()
            self._errors['somefield'].append('Some field is blank')

UPDATE:

更新:

From the Django docs:

Django的文档:

Each named form-field can be output to the template using {{ form.name_of_field }}, which will produce the HTML needed to display the form widget. Using {{ form.name_of_field.errors }} displays a list of form errors, rendered as an unordered list. This might look like:

每个命名的表单字段都可以使用{{{{{{form.name_of_field}}输出到模板,这将生成显示表单小部件所需的HTML。使用{{{{{{form.name_of_field.errors}显示一个表单错误列表,以无序列表的形式呈现。这样子:

<ul class="errorlist">
    <li>Sender is required.</li>
</ul>

The list has a CSS class of errorlist to allow you to style its appearance. If you wish to further customize the display of errors you can do so by looping over them (emphasis mine):

该列表具有errorlist的CSS类,允许您对其外观进行样式化。如果您希望进一步自定义错误的显示,可以通过循环显示它们(重点是我的):

{% if form.subject.errors %}
    <ol>
    {% for error in form.subject.errors %}
        <li><strong>{{ error|escape }}</strong></li>
    {% endfor %}
    </ol>
{% endif %}