Hi

Your date settings seem to only affect dates when they are being rendered.

When a date is input and later pushed into the database - a date
like 02/03/2013 is ambiguous as the database cannot determine whether you
mean 2nd Mar or 3rd Feb; dates like 02/26/2013 are not ambiguous. So
you get what seems like inconsistent behaviour.

You probably need to perform some format checking AFTER the form has
been posted. You can then use your date formatting switch to control the
change. There are several possible ways of doing this: e.g. create a
function to perform the conversion and call this from a clean method in
forms.py as part of the form validation

https://docs.djangoproject.com/en/1.5/ref/forms/validation/

An alternative approach would be to provide a js date picker like the one
used in the admin interface for date fields

On Fri, 5 Jul 2013 03:43:34 -0700 (PDT)
Sivaram R <sivaram2...@gmail.com> wrote:

> All
> 
> 1.My application consist of two part.Part I -  Settings page //just like 
> settings selection in iphone or mobile phone
>                                                      Part II - Event 
> creation page 
> 
>  So,in settings page ,in my app a page called datetime_format.html is
> for selecting date and time formats.This contains only two formats
> namely dd/mm/yyyy and mm/dd/yyyy.In forms.py i had mentioned 0 and 1 for 
> respective format.If user selected mm/dd/yyyy format in database,the
> value is stored as 1.Depend on the value in database i am process to
> change the format of date in my event creation page.Based on the format
> selected,the date format takes effect in event creation page.
> 
> My problem is,if the user selects the format as dd/mm/yyyy and click 
> save,no problem appears.
> 
> If the selected format is mm/dd/yyyy and if user tried to change the 
> date,the date and month gets interchange,for example if the date is 
> 02/03/2013 (3rd feb)in mm/dd/yyyy format after form post(after save in 
> database) the values become 03/02/2013 that is 2nd march.
> 
> But if the given date is 02/26/2013 that is (26th feb) after post it is 
> saving as such and no format interchange issue.
> 
> I want to know why this is happening and how to solve this.
> 
> Thanks
>  
>  
>                                          
> On Thursday, July 4, 2013 5:20:52 PM UTC+5:30, Sivaram R wrote:
> >
> > views.py
> >
> > def calender(request):
> >
> >     """"""
> >     settingsform = SettingsForm(instance=settings)
> >     if request.method == 'POST':
> >         reportform = ReportDateTimeForm(request.POST, instance=report)
> >         if reportform.is_valid():
> >             report = reportform.save(commit=False)
> >             report.user = request.user
> >             report.save()
> >     if settings and settings.date_format:
> >         date =
> > report.manual_date.strftime(reportform.fields['manual_date'].input_formats[1])
> > createddate = report.created_date_time.strftime('%b %d %Y') else:
> >         date =
> > report.manual_date.strftime(reportform.fields['manual_date'].input_formats[0])
> > createddate = report.created_date_time.strftime('%d %b %Y') """""" 
> >     return render_to_response('calender.html',
> >                                {
> >                                 'reportform': reportform,
> >                                 'settings': settings,
> >                                 'settingsform':settingsform
> >                                  },
> >                                  context_instance=RequestContext(request))
> >
> > forms.py
> >
> > DATE_FORMAT = (
> >     ('0', ' dd / mm / yyyy'),
> >     ('1', 'mm / dd / yyyy'),)
> > DATE_INPUT_FORMAT = ['%d/%m/%Y','%m/%d/%Y']
> > class ReportDateTimeForm(forms.ModelForm):
> >     manual_date = forms.DateField(input_formats = DATE_INPUT_FORMAT,
> >                       widget=forms.DateInput(format = '%d/%m/%Y'))
> > class SettingsForm(forms.ModelForm):
> >     date_format = forms.ChoiceField(widget=forms.RadioSelect(),
> > choices=DATE_FORMAT, initial='0')
> >
> >  
> >   views.py
> >
> > def calender(request):
> >
> >     """"""
> >     settingsform = SettingsForm(instance=settings)
> >     if request.method == 'POST':
> >         reportform = ReportDateTimeForm(request.POST, instance=report)
> >         if reportform.is_valid():
> >             report = reportform.save(commit=False)
> >             report.user = request.user
> >             report.save()
> >     if settings and settings.date_format:
> >         date =
> > report.manual_date.strftime(reportform.fields['manual_date'].input_formats[1])
> > createddate = report.created_date_time.strftime('%b %d %Y') else:
> >         date =
> > report.manual_date.strftime(reportform.fields['manual_date'].input_formats[0])
> > createddate = report.created_date_time.strftime('%d %b %Y') """""" 
> >     return render_to_response('calender.html',
> >                                {
> >                                 'reportform': reportform,
> >                                 'settings': settings,
> >                                 'settingsform':settingsform
> >                                  },
> >                                  context_instance=RequestContext(request))
> >
> > forms.py
> >
> > DATE_FORMAT = (
> >     ('0', ' dd / mm / yyyy'),
> >     ('1', 'mm / dd / yyyy'),)
> > DATE_INPUT_FORMAT = ['%d/%m/%Y','%m/%d/%Y']
> > class ReportDateTimeForm(forms.ModelForm):
> >     manual_date = forms.DateField(input_formats = DATE_INPUT_FORMAT,
> >                       widget=forms.DateInput(format = '%d/%m/%Y'))
> > class SettingsForm(forms.ModelForm):
> >     date_format = forms.ChoiceField(widget=forms.RadioSelect(),
> > choices=DATE_FORMAT, initial='0')
> >
> > If no values are saved in settings table,it is showing the default
> > date format.If any value is saved eg:if the selecetd format is
> > dd/mm/yyyy in db it is saved as 0.But in calender page it is displayed
> > as mm/dd/yyyyformat,it is showing mm/dd/yyyy format for both date
> > format,it seems like for default it is showing the correct format,if
> > any values are saved it is always showing this format mm/dd/yyyy
> >
> > Any help is greatly appreciated.
> >
> >
> >
> 



-- 
Drew Ferguson
AFC Commercial
http://www.afccommercial.co.uk

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-users+unsubscr...@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to