Javascript django TypedChoiceField-不允许默认值为0
对于TypedChoiceField,并且不允许用户输入选定的值“0”,我不确定是否正确设计了forms.py文件 我读过这本书,但找不到关于这本书的任何东西。有一些关于空值的东西,但是我找不到任何关于TypedChoiceField的例子 我在选择列表中有一个国家名称列表:Javascript django TypedChoiceField-不允许默认值为0,javascript,jquery,django-forms,django-templates,parsley.js,Javascript,Jquery,Django Forms,Django Templates,Parsley.js,对于TypedChoiceField,并且不允许用户输入选定的值“0”,我不确定是否正确设计了forms.py文件 我读过这本书,但找不到关于这本书的任何东西。有一些关于空值的东西,但是我找不到任何关于TypedChoiceField的例子 我在选择列表中有一个国家名称列表: <select data-parsley-required="true" name="address_country_style_type" data-parsley-required-message="This f
<select data-parsley-required="true" name="address_country_style_type" data-parsley-required-message="This field is required." id="id_address_country_style_type" data-parsley-id="1440" class="input-xxlarge" data-original-title="" title="">
<option value="0"> Select a country or territory</option>
<option value="1">Afghanistan</option>
<option value="2">Aland Islands</option>
<option value="3">Albania</option>
......
</select>
我已经添加了设置客户端验证的功能
在forms.py文件中的“address\u country\u style\u type=forms.TypedChoiceField”声明中是否有方法声明address\u country\u style\u type不能接受选项值0,因此,django parsley将自动获取此验证,并且我可以在语句中剔除已清理的数据?我没有使用django,因此我只能帮助您使用parsleyjs 如果您设法删除
value=“0”
并将其像value=”“
那样放置,Parsleyjs将告诉您该字段是必需的:
选择一个国家或地区
@parsleyfy
class AddressDetailsForm(forms.Form):
required_css_class = 'required'
address_country_style_type = forms.TypedChoiceField(
coerce=int,
label=_('Address Format'),
choices=[(x, x) for x in range(0, 256)],
required=True)
....
def clean(self):
cd_addf = super(AddressDetailsForm, self).clean()
if 'address_country_style_type' in cd_addf and cd_addf['address_country_style_type'] == 0:
self._errors['address_country_style_type'] = self.error_class([_("You must select an Address Format.")])
del self.cleaned_data['address_country_style_type']
else:
....
<option value=""> Select a country or territory</option>