我有django形式的字段需要整数。 字段中的默认值为' - '。 这意味着当用户忽略该字段时,将返回表单字段错误。 令人讨厌的是,这条消息令人讨厌,而且这个消息很烦人。在里面。
如何摆脱U?
html
<div class="col-sm-1">
{% with childForm.age as field %}
<div class="form-group{% if field.errors %} has-error{% endif %}" id="children-age">
{{ field }}
{% if field.errors %}<p class="help-block">{% for error in field.errors %}{{ error }}{% endfor %}</p>{% endif %}
</div>
{% endwith %}
</div>
错误
forms.py
class TravelGeneralEnquiryForm(forms.ModelForm):
children = forms.ChoiceField(choices=(('','-'),) + tuple(((str(x), x) for x in range(0, 11))), required=True)
adults = forms.ChoiceField(choices=(('','-'),) + tuple(((str(x), x) for x in range(1, 11))), required=True)
pets = forms.ChoiceField(choices=((str(x), x) for x in range(0, 11)), required=True, initial=1)
budget = forms.ChoiceField(choices=BUDGET,required=False)
interest_destination = forms.MultipleChoiceField(required=False,
widget=forms.CheckboxSelectMultiple,
choices=INTEREST_DESTINATION_CHOICES)
interest_accommodation = forms.MultipleChoiceField(required=False,
widget=forms.CheckboxSelectMultiple,
choices=INTEREST_ACCOMMODATION_CHOICES)
contact_preference = forms.ChoiceField(required=False,
widget=forms.RadioSelect,
choices=CONTACT_PREFERENCE)
inspire_hours = forms.ChoiceField(choices=INSPIRE_HOURS, required=False)
def __init__(self, *args, **kwargs):
super(TravelGeneralEnquiryForm, self).__init__(*args, **kwargs)
self.fields['name_title'].widget.attrs = {'class': 'form-control',}
self.fields['name_title'].label = 'Title*'
self.fields['first_name'].widget.attrs = {'class': 'form-control',}
self.fields['first_name'].label = 'First name*'
self.fields['last_name'].widget.attrs = {'class': 'form-control',}
self.fields['last_name'].label = 'Last name*'
self.fields['email'].widget.attrs = {'class': 'form-control',}
self.fields['email'].label = 'Email address*'
self.fields['telephone'].widget.attrs = {'class': 'form-control',}
self.fields['telephone'].label = 'Telephone number*'
self.fields['check_in_date'].widget.attrs = {'class': 'form-control datepicker', 'data-date-format': 'dd/mm/yyyy', 'placeholder': 'dd/mm/yyyy', 'autocomplete': 'off',}
self.fields['check_in_date'].widget.format = '%d/%m/%Y'
self.fields['check_in_date'].input_formats = ['%d/%m/%Y']
self.fields['check_in_date'].label = 'Check in date*'
self.fields['check_out_date'].widget.attrs = {'class': 'form-control datepicker', 'data-date-format': 'dd/mm/yyyy', 'placeholder': 'dd/mm/yyyy', 'autocomplete': 'off',}
self.fields['check_out_date'].widget.format = '%d/%m/%Y'
self.fields['check_out_date'].input_formats = ['%d/%m/%Y']
self.fields['check_out_date'].label = 'Check out date*'
self.fields['budget'].widget.attrs = {'class': 'form-control'}
self.fields['info'].widget.attrs = {'class': 'form-control', 'rows': '3', 'placeholder': 'Pet breed/size/temperament/food allergies/disabled access'}
self.fields['adults'].widget.attrs = {'class': 'form-control'}
self.fields['adults'].label = 'Number of adults*'
self.fields['children'].widget.attrs = {'class': 'form-control', 'initial': '---'}
self.fields['children'].label = 'Number of children*'
self.fields['pets'].widget.attrs = {'class': 'form-control'}
self.fields['pets'].label = 'Number of pets*'
self.fields['rooms'].widget.attrs = {'class': 'form-control'}
self.fields['rooms'].label = 'Number of rooms*'
self.fields['contact_preference'].label = 'Prefered method of contact'
self.fields['inspire_postcode'].label = 'Your Postcode'
self.fields['inspire_postcode'].widget.attrs = {'class': 'form-control', 'placeholder': 'N1 4SZ'}
self.fields['inspire_hours'].label = 'How far would you like to travel?'
self.fields['inspire_hours'].widget.attrs = {'class': 'form-control'}
self.fields['inspire_details'].widget.attrs = {'class': 'form-control', 'rows': '2', 'cols':50 , 'placeholder': 'The more information you give us, the better we are able to help you!'}
self.fields['inspire_details'].label = 'Additional Information'
class Meta:
model = TravelGeneralEnquiry
exclude = ('enquiry_source',)
class TravelGeneralEnquiryChildForm(forms.ModelForm):
age = forms.ChoiceField(choices=(('-','-'),) + tuple(((str(x), x) for x in range(0, 18))))
def __init__(self, *args, **kwargs):
super(TravelGeneralEnquiryChildForm, self).__init__(*args, **kwargs)
self.fields['age'].widget.attrs = {'class': 'form-control'}
class Meta:
model = TravelGeneralEnquiryChild
fields = ('age',)
class TravelGeneralEnquiryChild(models.Model):
general_enquiry = models.ForeignKey(TravelGeneralEnquiry, blank=False, null=False, related_name="general_enquiry_children")
age = models.CharField(max_length=2, default=0, null=False, blank=False, choices=((str(x), x) for x in range(0, 18)))
答案 0 :(得分:1)
我不确定错误消息为何具有u''
前缀。我建议您将选项的值更改为空字符串''
。
class TravelGeneralEnquiryChildForm(forms.ModelForm):
age = forms.ChoiceField(choices=(('','-'),) + tuple(((str(x), x) for x in range(0, 18))))
这样您就会收到错误消息This field is required.
,而不是无效的选择错误消息。