【问题标题】:How to remove error message in django forms?如何删除 django 表单中的错误消息?
【发布时间】:2014-07-04 18:59:06
【问题描述】:

我已经找了几个小时了如何删除出现在我的评论表单上的错误消息。
例如,当有人单击带有空文本的“发送您的按钮”时,会出现以下错误消息“此字段为必填项”。
我希望此字段为必填但不显示错误消息。

代码如下:

HTML

<form action="/s={{ s }}/a={{ a }}/comment/q={{ q }}/" method="post">{% csrf_token %}{{ form.as_p }}
<button type="submit">Send your comment</button>
</form>

DJANGO

my_default_errors_comment = {
}

class CommentForm(forms.Form):
    required_css_class = 'required'
    message = forms.CharField(widget=forms.Textarea, label=u'', max_length=200, required=True, error_messages=my_default_errors_comment)

即使我删除了“my_default_errors_comment”的东西,错误信息仍然出现。

有人可以帮忙吗? 注意:我所需的 css 类只是在字段后添加一个星号。

非常感谢您!

【问题讨论】:

  • 你应该展示你的观点。似乎您错误地实例化了表单。

标签: python django forms


【解决方案1】:

你可以这样实现:

来自Django docs

<form action="/s={{ s }}/a={{ a }}/comment/q={{ q }}/" method="post">{% csrf_token %}

    {% for field in form %}
        <div class="fieldWrapper">
            {# uncomment to display field errors #}
            {# {{ field.errors }} #} 

            {{ field.label_tag }} {{ field }}
        </div>
    {% endfor %}
    <button type="submit">Send your comment</button>
</form>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-19
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    • 2019-11-17
    • 1970-01-01
    • 2016-09-04
    • 2015-05-28
    相关资源
    最近更新 更多