未呈现My django自定义表单验证错误
在我的项目中,我将对表单中的某些字段应用一些自定义验证,在表单类中重写clean方法,但是当我简单地呈现错误本身时,它会显示在模板中,但当我将其呈现为文本时,它不会显示 形式 模型 模板未呈现My django自定义表单验证错误,django,django-forms,Django,Django Forms,在我的项目中,我将对表单中的某些字段应用一些自定义验证,在表单类中重写clean方法,但是当我简单地呈现错误本身时,它会显示在模板中,但当我将其呈现为文本时,它不会显示 形式 模型 模板 {%if保险\表单错误%} {保险表格中的错误为%。{errors%} {{error.as_text} {%endfor%} {%endif%} class InsuranceInformationForm(forms.ModelForm): expiration_date = forms.Date
{%if保险\表单错误%}
{保险表格中的错误为%。{errors%}
{{error.as_text}
{%endfor%}
{%endif%}
class InsuranceInformationForm(forms.ModelForm):
expiration_date = forms.DateField(widget=forms.SelectDateWidget(years=years))
class Meta:
model = InsuranceInformation
exclude = ('patient',)
def clean(self):
cleaned_data = super().clean()
carrier = cleaned_data.get('insurance_carrier')
insurance_type = cleaned_data.get('type_of_insurance')
if (carrier and not insurance_type) or (insurance_type and not carrier):
self._errors['Insurance'] = self.error_class(["Insurance information incomplete."])
return cleaned_data
class InsuranceInformation(models.Model):
INSURANCE_TYPE_CHOICES = (
('MEDICAL', 'Medical'),
)
insurance_carrier = models.OneToOneField(InsuranceCarrier, on_delete=models.CASCADE, blank=True, null=True,
verbose_name='insurance carrier', related_name='insurance', unique=True)
type_of_insurance = models.CharField('insurance type', max_length=50, blank=True, null=True,
help_text='Type of insurance', choices=INSURANCE_TYPE_CHOICES)
expiration_date = models.DateField('insurance date expiration', help_text='insurance date expiration')
patient = models.OneToOneField(Patient, on_delete=models.CASCADE, blank=True, null=True,
verbose_name='insurance owner', related_name='insurance')
def __str__(self):
return str(self.patient)+ "'s" + ' ' + 'Insurance Information'
{%if insurance_form.errors%}
{%for error in insurance_form.errors%}
<p class="error">{{error.as_text}}</p>
{%endfor%}
{%endif%}