Django forms dateField does not validate

I am trying to validate a user profiling form in django and I cannot. Something seems to be wrong with forms.dateField (). It does not check (i.e. is_valid () returns false)

these are my forms dateField: date_of_birth = forms.DateField(label=u'date of birth', input_formats='%d/%m/%Y', required=False, widget=forms.DateInput(format = '%d/%m/%Y'))

I noticed that request.POST.get('date_of_birth', '') returns the correct date (i.e. the date I typed in the html form field).

I also noticed that in this function:

 def clean_date_of_birth(self): date = self.cleaned_data['date_of_birth'] 

the date object is always missing.

What am I doing wrong?

EDIT:

This is what I am trying to enter: 29/07/1974 (July 29, 1974)

This is the output of "submit" (various requests)

 29/07/1974 profile form is *NOT* valid [23/Feb/2012 12:16:27] "POST /profile/chris/ HTTP/1.1" 200 16289 29/7/1974 profile form is *NOT* valid [23/Feb/2012 12:16:33] "POST /profile/chris/ HTTP/1.1" 200 16289 1974-07-29 profile form is *NOT* valid [23/Feb/2012 12:18:15] "POST /profile/chris/ HTTP/1.1" 200 16289 

This is my template.

  <div class="input_area"> <form id="profile_form" method="post" action="/profile/{{ user.username }}/">{% csrf_token %} {{ form.as_p }} <input type="submit" id="submit" value="save" class="submitButton idle" style="width:70px" /> </form> </div> 

this is my view.py

 def profile(request, username): form = ProfileForm(request.POST) print request.POST.get('date_of_birth', 'None') try: user = User.objects.get(username=username) except User.DoesNotExist: raise Http404(u'User not Found') if form.is_valid(): print 'profile form is valid' else: print 'profile form is *NOT* valid' 

and finally, these are my forms.py (don't use clean_data functions at the moment)

 class ProfileForm(forms.Form): tz = [] timezones = Timezone.objects.all() for timezone in timezones: val = str(timezone.hour) v = val.split(':') tuple = (timezone.id, '('+timezone.sign+''+v[0]+':'+v[1]+') '+timezone.name) tz.append(tuple) sex = [('male','male'),('female', 'female'),('unknown', 'prefer not to say')] real_name = forms.CharField(label=u'real name', widget=forms.TextInput, required=False) date_of_birth = forms.DateField(label=u'date of birth', input_formats='%d/%m/%Y', required=False, widget=forms.DateInput(format = '%d/%m/%Y')) pp_email = forms.EmailField(label=u'Paypal Email', widget=forms.TextInput, required=False) gender = forms.ChoiceField(label=u'sex', choices=sex, widget=forms.Select(), required=False) timezone = forms.ChoiceField(label=u'time zone', choices=tz, widget=forms.Select()) address = forms.CharField(label=u'street address', widget=forms.Textarea, required=False) postal = forms.CharField(label=u'postal code', widget=forms.TextInput, required=False) 
+4
source share
2 answers
+6
source

With Django 1.6 and above, you can use localized_fields in your Meta form or localize=True in your form. See https://docs.djangoproject.com/en/1.9/topics/i18n/formatting/#format-localization .

When using USE_L10N = True , Django will use the formats.py file for your locale (part of LANGUAGE_CODE ).

You can get something DRY like this (since the fields specified in models.py do not need to be repeated in forms.py ):

 class SomeForm(forms.Form): class Meta: model = SomeModel fields = ('first_name', 'dob',) localized_fields = ('dob',) 
+1
source

Source: https://habr.com/ru/post/1397985/


All Articles