我是django的新手,这个问题令我难过。我看不到错误,现在看了好几个小时!
我试图根据下拉列表的值关闭或打开两个文本字段的验证。如果用户选择索引值8888或9999,则关闭验证 - 这似乎正常(woo hoo !!)。
当用户选择另一个值(不是8888或9999)时,验证将打开,但即使我填写了相关的文本字段,也会保持打开状态。即使填写了文本字段,表单也不会提交,因为仍然会触发验证。
以下是我的forms.py页面中的代码:
def clean(self):
cd_wrtdf = super(WorkplaceRelatedTrainingDetailsForm, self).clean()
if cd_wrtdf['workplace_training_display_type'] == 8888 or cd_wrtdf['workplace_training_display_type'] == 9999:
self.cleaned_data['workplace_training_institute'] = ''
self.cleaned_data['workplace_training_date_completed'] = ''
self.cleaned_data['workplace_training_date_expiry'] = ''
else:
if cd_wrtdf['workplace_training_display_type'] == 0:
self._errors['workplace_training_institute'] = self.error_class([_("This field is required.xxxxxxxxx")])
del self.cleaned_data['workplace_training_institute']
self._errors['workplace_training_date_completed'] = self.error_class([_("This field is required.yyyyyyyy")])
del self.cleaned_data['workplace_training_date_completed']
return cd_wrtdf
非常感谢任何建议或代码示例。
感谢。
答案 0 :(得分:0)
最后,验证工作正常进行!
这是代码(希望这有助于某人):
def clean(self):
cd_wrtdf = super(WorkplaceRelatedTrainingDetailsForm, self).clean()
if cd_wrtdf['workplace_training_display_type'] == 8888 or cd_wrtdf['workplace_training_display_type'] == 9999:
self.cleaned_data['workplace_training_institute'] = ''
self.cleaned_data['workplace_training_date_completed'] = ''
self.cleaned_data['workplace_training_date_expiry'] = ''
else:
if 'workplace_training_institute' in cd_wrtdf and len(cd_wrtdf['workplace_training_institute'].strip()) == 0:
self._errors['workplace_training_institute'] = self.error_class([_("This field is required.xxxxxxxxx")])
del self.cleaned_data['workplace_training_institute']
if 'workplace_training_date_completed' in cd_wrtdf and len(cd_wrtdf['workplace_training_date_completed'].strip()) == 0:
self._errors['workplace_training_date_completed'] = self.error_class([_("This field is required.yyyyyyyy")])
del self.cleaned_data['workplace_training_date_completed']
return cd_wrtdf