Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/django/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在django中将自定义表单错误分配给modelform中的字段_Django_Forms - Fatal编程技术网

在django中将自定义表单错误分配给modelform中的字段

在django中将自定义表单错误分配给modelform中的字段,django,forms,Django,Forms,我试图将自定义表单错误分配给django中modelform中的一个字段,以便在出现“标准”错误的地方出现,例如字段留空,格式相同(由crispy forms处理) 我的模型表单清理方法如下所示: def clean(self): cleaned_data = super(CreatorForm, self).clean() try: if cleaned_data['email'] != cleaned_data['re_email']:

我试图将自定义表单错误分配给django中modelform中的一个字段,以便在出现“标准”错误的地方出现,例如字段留空,格式相同(由crispy forms处理)

我的模型表单清理方法如下所示:

def clean(self):
    cleaned_data = super(CreatorForm, self).clean()
    try:
        if cleaned_data['email'] != cleaned_data['re_email']:
            raise forms.ValidationError({'email': "Your emails don't match!"})
    except KeyError:
        pass
    return cleaned_data
{{creator_form|crispy}}
在我的模板中,我显示表单/重新提交的表单,如下所示:

def clean(self):
    cleaned_data = super(CreatorForm, self).clean()
    try:
        if cleaned_data['email'] != cleaned_data['re_email']:
            raise forms.ValidationError({'email': "Your emails don't match!"})
    except KeyError:
        pass
    return cleaned_data
{{creator_form|crispy}}
如果可能的话,我希望错误显示在re_电子邮件字段的下方(尽管目前我认为我会更幸运地将其显示在电子邮件字段下方。目前它显示在表单顶部,未格式化

对于re_电子邮件字段,尽管不是模型的一部分,但在re_电子邮件字段下方会显示保留为空的错误。我如何将错误“附加”到字段,使其显示在字段下方/附近


感谢所有帮助

要使错误显示在特定字段上,您需要明确定义错误发生的字段,因为您正在覆盖
.clean()
。以下是从中获取的示例: