【发布时间】:2021-02-15 00:10:13
【问题描述】:
我在我的一个表单上使用自定义 clean() 函数来满足条件字段要求。作为 DOM 的一部分,我可以使用 {{ form.non_field_errors }} 在我的模板上提出验证错误。我希望像默认错误一样弹出验证错误,但不知道如何。
这里是自定义的clean()函数:
class ReportForm(forms.Form):
def clean(self):
cleaned_data = super().clean()
class_type = cleaned_data.get("class_type")
class_other = cleaned_data.get("class_other")
if class_type == "Other":
if not class_other:
msg = "Please fill out this field."
raise ValidationError(msg)
我不知道在视图或模板中放置什么以使其显示为弹出窗口 - 我目前没有任何特别之处。 Django可以吗,还是我需要使用javascript?
【问题讨论】:
标签: python django django-views django-forms django-templates