Django ChoicesField分割器?
我在表单中使用了一个Django ChoicesField分割器?,django,django-forms,Django,Django Forms,我在表单中使用了一个ChoicesField,但我想在其中添加一个分隔符,如下所示: COUNTRIES = ( ('CA', _('Canada')), ('US', _('United States')), (None, _('---')), # <---------- ('AF', _('Afghanistan')), ('AX', _('Aland Islands')), ('AL', _('Albania')), ('DZ'
ChoicesField
,但我想在其中添加一个分隔符,如下所示:
COUNTRIES = (
('CA', _('Canada')),
('US', _('United States')),
(None, _('---')), # <----------
('AF', _('Afghanistan')),
('AX', _('Aland Islands')),
('AL', _('Albania')),
('DZ', _('Algeria')),
('AS', _('American Samoa')),
# ...
class AddressForm(forms.Form):
country = forms.ChoiceField(choices=COUNTRIES, initial='CA')
COUNTRIES = [
("CA", _("Canada")),
("US", _("United States")),
# This format will indent the nested tuples over and the
# "-----" will be un-selectable
#
("---------------", ( # This will be a header for the items nested below
("AF", _("Afghanistan")),
('AX', _('Aland Islands')),
('AL', _('Albania')),
('DZ', _('Algeria')),
('AS', _('American Samoa')),
)
),
]
国家=(
(‘CA’、‘加拿大’),
(‘美国’、‘美国’),
(无、('--')、#如果选择了任何分隔符,则可以编写一个干净的方法来引发验证错误
class AddressForm(forms.Form):
country = forms.ChoiceField(choices=COUNTRIES, initial='CA')
def clean_country(self):
data = self.cleaned_data["country"]
if not data:
raise forms.ValidationError("You must select a valid country.")
return data
如果选择了任何分隔符,您可以编写一个clean方法来引发验证错误
class AddressForm(forms.Form):
country = forms.ChoiceField(choices=COUNTRIES, initial='CA')
def clean_country(self):
data = self.cleaned_data["country"]
if not data:
raise forms.ValidationError("You must select a valid country.")
return data
您可以使用以下选项:
您可以将其格式化为以下格式:
COUNTRIES = (
('CA', _('Canada')),
('US', _('United States')),
(None, _('---')), # <----------
('AF', _('Afghanistan')),
('AX', _('Aland Islands')),
('AL', _('Albania')),
('DZ', _('Algeria')),
('AS', _('American Samoa')),
# ...
class AddressForm(forms.Form):
country = forms.ChoiceField(choices=COUNTRIES, initial='CA')
COUNTRIES = [
("CA", _("Canada")),
("US", _("United States")),
# This format will indent the nested tuples over and the
# "-----" will be un-selectable
#
("---------------", ( # This will be a header for the items nested below
("AF", _("Afghanistan")),
('AX', _('Aland Islands')),
('AL', _('Albania')),
('DZ', _('Algeria')),
('AS', _('American Samoa')),
)
),
]
您可以使用以下选项:
您可以将其格式化为以下格式:
COUNTRIES = (
('CA', _('Canada')),
('US', _('United States')),
(None, _('---')), # <----------
('AF', _('Afghanistan')),
('AX', _('Aland Islands')),
('AL', _('Albania')),
('DZ', _('Algeria')),
('AS', _('American Samoa')),
# ...
class AddressForm(forms.Form):
country = forms.ChoiceField(choices=COUNTRIES, initial='CA')
COUNTRIES = [
("CA", _("Canada")),
("US", _("United States")),
# This format will indent the nested tuples over and the
# "-----" will be un-selectable
#
("---------------", ( # This will be a header for the items nested below
("AF", _("Afghanistan")),
('AX', _('Aland Islands')),
('AL', _('Albania')),
('DZ', _('Algeria')),
('AS', _('American Samoa')),
)
),
]
我不知道您使用的是Django的哪个版本,但我使用的是1.10.1,我使用了以下内容:
ICONS = (
(None, ''),
(None, '==Extra Contact Info=='),
('phone', 'Phone'),
('phone', 'Phone (square)'),
('fax', 'Fax'),
('envelope', 'E-mail (black)'),
('envelope-o', 'E-mail (white/clear)'),
(None, ''),
(None, '==Social Media=='),
('facebook', 'Facebook'),
('facebook-official', 'Facebook (official)'),
('facebook-square', 'Facebook (square)'),
('google-plus', 'Google Plus'),
...
)
这就是我所用的,如果用户在下拉菜单中选择了任何一个列表项,其中有一个“无”值,它会对用户咆哮说“这个字段是必需的。”
现在…当然,在我的项目中,选项列表正在我的whatever.com/admin页面中使用,但这可能不相关。然而,相关的是,您必须确保您的模型(或表单)类字段不包含“blank=True”。默认情况下,如果您省略它,它应该为false,换句话说,该字段将不接受null或空字符串值。这应该是您所需要的全部…我不知道您使用的是哪个版本的Django,但我使用的是1.10.1,我使用了以下内容:
ICONS = (
(None, ''),
(None, '==Extra Contact Info=='),
('phone', 'Phone'),
('phone', 'Phone (square)'),
('fax', 'Fax'),
('envelope', 'E-mail (black)'),
('envelope-o', 'E-mail (white/clear)'),
(None, ''),
(None, '==Social Media=='),
('facebook', 'Facebook'),
('facebook-official', 'Facebook (official)'),
('facebook-square', 'Facebook (square)'),
('google-plus', 'Google Plus'),
...
)
这就是我所用的,如果用户在下拉菜单中选择了任何一个列表项,其中有一个“无”值,它会对用户咆哮说“这个字段是必需的。”
现在…当然,在我的项目中,选项列表正在我的whatever.com/admin页面中使用,但这可能不相关。然而,相关的是,您必须确保您的模型(或表单)类字段不包含“blank=True”。默认情况下,如果忽略它,则该字段应为false,换句话说,该字段将不接受null或空字符串值。这应该是您所需要的全部…我不希望其他字段缩进,只是分开。老实说,我更希望它们不缩进,但我还没有找到一种方法来制作不可选择的标题,并且不将其索引结束了。我只是希望你不必为这个方法编写任何验证逻辑。也许有某种css规则可以用来将这些内容转换过来?我不想让其他内容缩进,只是分开。我真的希望它们不缩进,但我还没有找到一种方法来制作不可选择的标题和内容没有缩进。我只是喜欢你不必为这个方法编写任何验证逻辑。也许有某种css规则可以用来将它们覆盖?