python - Django field error messages -
i have fields in django form require integer. default value in field '-'. means when user ignores field, form field error returned. annoyingly message has annoying 'u' in it.
how rid of u?
the html
<div class="col-sm-1"> {% childform.age field %} <div class="form-group{% if field.errors %} has-error{% endif %}" id="children-age"> {{ field }} {% if field.errors %}<p class="help-block">{% error in field.errors %}{{ error }}{% endfor %}</p>{% endif %} </div> {% endwith %} </div>
the error
forms.py
class travelgeneralenquiryform(forms.modelform): children = forms.choicefield(choices=(('','-'),) + tuple(((str(x), x) x in range(0, 11))), required=true) adults = forms.choicefield(choices=(('','-'),) + tuple(((str(x), x) x in range(1, 11))), required=true) pets = forms.choicefield(choices=((str(x), x) 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 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 give us, better able 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) 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) x in range(0, 18)))
i'm not sure why error message has u''
prefix. suggest change value of choice empty string ''
.
class travelgeneralenquirychildform(forms.modelform): age = forms.choicefield(choices=(('','-'),) + tuple(((str(x), x) x in range(0, 18))))
that way you'll error message this field required.
instead of invalid choice error message.
Comments
Post a Comment