I have an input form defined as follows.
class ProfileForm(forms.ModelForm):
class Meta:
model = Profile
fields = ['first_name', 'last_name', 'language',
]
widgets = {
'language': forms.RadioSelect,
}
Inside my models.py
I define the following:
first_name = models.CharField(max_length=250)
last_name = models.CharField(max_length=250)
LANGUAGE = (('AR', 'Arabic'), ('FR', 'French'), ('ES', 'Spanish'))
language = models.CharField(max_length=20, choices=LANGUAGE, blank=False, default=None)
The first_name and last_name are required fields, and if the user leaves these two options empty, a warning message will prompt. However, I also want the language
field to be a required field. To do so, I set blank=False
. While it is correct that if you submit the form without selecting an option for language
, the form will be invalid. However, it does not give the user a warning about it. How can this be done?
According to the documentation , if you set blank=False
, you can not use default=None
.
Unless blank=False is set on the field along with a default then a label containing "---------" will be rendered with the select box. To override this behavior, add a tuple to choices containing None; eg (None, 'Your String For Display'). Alternatively, you can use an empty string instead of None where this makes sense - such as on a CharField.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.