我一直在尝试让它工作,但失败了。我使用了- help_text,blank_label,default等,它们都导致了错误。我有一张表格,让你选择你的国籍。模型工作得很好,在管理部分也是如此。这些国家是使用Django-countries包生成的。但是,当我呈现表单时,缺省值是阿富汗。现在,我希望默认设置为“选择一个国家”,然后用户可以从下拉菜单中进行选择。drop本身运行得很好。我该怎么做呢?(PS-我在这里检查了各种问题,但他们没有回答以下问题:Django: Country drop down list?,set up form option as default in a drop down,django countries dropdown default等)
从31/08/17开始,我甚至添加了Meta来改变占位符的值。虽然这改变了其他字段的值,但并没有影响国家字段。请参阅下面的更新代码。
从form.py代码片段
nationality = LazyTypedChoiceField(choices=countries, label='Nationality', required=False)
class Meta:
model = ProfileTeacher
fields = ('description', 'document' 'name','last_name','phone_number', 'date_of_birth', 'bank_account', 'id_number', 'postal_code', 'adress', 'profile_image', 'nationality' )
def __init__(self, *args, **kwargs):
super(RegisterFormTeacher, self).__init__(*args, **kwargs)
self.fields['nationality'].widget.attrs['placeholder'] = 'Select a Country'来自model.py
nationality = CountryField(blank_label='(select country)', null=True, blank = True)发布于 2018-07-23 02:08:09
事实证明,它相当简单。在form.py中,我需要将其更改为:
nationality = LazyTypedChoiceField(choices=countries, label='Nationality', required=False)至
nationality = CountryField(blank_label='(Select country)',).formfield()在.formfield()中,您可以设置其他属性,如'required‘等。
https://stackoverflow.com/questions/45811976
复制相似问题