django表单验证:未显示字段错误



我迷失了表单验证和显示错误消息的方法

我有一个";定制的";表单显示在我的模板中(逐字段显示(

我需要在两个字段之间进行验证,所以我应该使用clean方法如果勾选了复选框,则文本输入不能为空我想将错误消息绑定到我的文本字段,但错误消息不显示。。。

forms.py

class CreateForm(forms.Form):
def __init__(self, request, *args, **kwargs):
super(CreateForm, self).__init__(*args, **kwargs)
self.fields["bra_00A_act"] = forms.BooleanField(label = "", required = False)
self.fields["bra_00A_lib"] = forms.CharField(label = "", required = False)
def clean(self):
cleaned_data = super(CreateForm, self).clean()
if cleaned_data["bra_00A_act"] and cleaned_data["bra_00A_lib"] == "":
self.add_error('bra_00A_lib', "custom error message")
raise forms.ValidationError('error')
return cleaned_data

模板html

<form id="randomization_settings_form" method="POST" class="post-form">
{% csrf_token %}
<input id="new_parameters" type="hidden" name="parameters" value="">
{{ form.non_field_errors }}
<table id="table_parametrage" class="table table-hover">
<thead>
<tr>
<th>Bras A</th>
<th>-----</th>
</tr>
<tr>
<td colspan="2">
<div class="fieldWrapper">
{{ form.bra_00A_act.errors }}
{{ form.bra_00A_act }}
{{ form.bra_00A_lib.errors }}
{{ form.bra_00A_lib }}
</div>
</td>
</tr>
</thead>
</table>
...
</form>
<script type="text/javascript">
$(document).ready(function () {
$("form").validate();
$("form").submit(function (e) {
if ($("form").valid()) {
}
});
})

相关内容

  • 没有找到相关文章

最新更新