【问题标题】:Why does field.errors display duplicate error messages from clean() method in Django form?为什么 field.errors 在 Django 表单中显示来自 clean() 方法的重复错误消息?
【发布时间】:2020-06-26 06:44:08
【问题描述】:

无论我使用clean() 还是clean_initial_ph(),我都会收到重复的错误消息。

如果我遍历表单字段:

   {% for field in form %}
        {{ field|as_crispy_field }}
        {% if form.errors %}
            {% for error in field.errors %}
                <div class="alert alert-danger">
                    <strong>{{ error|escape }}</strong>
                </div>
                {% endfor %}
                {% endif%}          
        {% endfor %}  

或手动列出字段和错误:

...
            {{ form.initial_ph|as_crispy_field }}
            {{ form.initial_ph.errors }}
...

我仍然收到重复的错误消息(尽管较大的错误消息的样式不同)。

我关注了validation guidelines。例如在clean_initial_ph() 我的验证看起来像这样:

    def clean_initial_ph(self):
        initial_ph = self.cleaned_data['initial_ph']
        if initial_ph:
            if initial_ph < 0:
                raise forms.ValidationError(_("PH must be above 0."), code = 'negative_intial_ph')
        return initial_ph

我对@9​​87654332@ 中的某些时间字段进行了另一个验证,它不显示重复的错误消息:

# Assuming clean_initial_ph is commented out
    def clean(self):
        cleaned_data = super().clean()
        pump_time = cleaned_data.get("pump_time")
        disposal_time = cleaned_data.get("disposal_time")
        incorptime = cleaned_data.get("incorptime")
        initial_ph = cleaned_data.get("initial_ph")
        disposal_ph = cleaned_data.get("disposal_ph")
        if pump_time and disposal_time: # DOES NOT DISPLAY DUPLICATE ERROR MESSAGES
            if pump_time >= disposal_time:
                self.add_error('pump_time','Pump time must be prior to disposal time.')
                self.add_error('disposal_time','Disposal time must be after to pump time.')
            if incorptime:
                if incorptime <= disposal_time:
                    self.add_error('incorptime','Incorp time must be after to disposal time.')
        if initial_ph: # DOES DISPLAY DUPLICATE ERROR MESSAGES
            if initial_ph < 0:
                self.add_error('initial_ph','PH must be above 0.')
        if disposal_ph: # DOES DISPLAY DUPLICATE ERROR MESSAGES
            if disposal_ph < 0:
                self.add_error('disposal_ph','PH must be above 0.')   

这里发生了什么?欢迎任何想法 - 谢谢!

【问题讨论】:

  • 您同时使用 clean 和 clean_field_name 方法?如果您在 clean_field_name 方法中验证,则不应在 clean 中再次验证,反之亦然。实际上,在 clean 中,您应该仅在需要多个字段以确保数据完整性时才进行验证。
  • 我实际上并没有同时使用它们 - 我只是尝试了两个选项调试。

标签: python django forms validation


【解决方案1】:

您应该检查字段是否有错误,而不是表单。

{% for field in form %}
    {{ field|as_crispy_field }}
    {% if field.errors %} // not form
        {% for error in field.errors %}
            <div class="alert alert-danger">
                <strong>{{ error|escape }}</strong>
            </div>
        {% endfor %}
    {% endif%}          
{% endfor %}

【讨论】:

  • 感谢您发现这一点 - 它没有解决我的问题,但绝对是检查错误的正确方法。
  • 您在检查 field.errors 后仍然收到重复的错误消息?
  • 是的 - 我在第三行将表单更改为字段。而且我仍然得到重复的错误。谢谢。
  • 你能分享你的完整代码,包括表单定义和视图吗?错误消息重复的唯一情况是同一个验证器对同一字段使用两次。
【解决方案2】:

我遇到了同样的问题并找到了解决方案:您必须将错误消息“分配”到 clean() 函数中的字段:

raise ValidationError({"field_name": _("Invalid value")}

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
猜你喜欢
  • 2019-11-09
  • 2013-09-27
  • 2020-12-02
  • 1970-01-01
  • 2019-04-26
  • 1970-01-01
  • 1970-01-01
  • 2021-03-04
  • 2021-08-28
相关资源
最近更新 更多