Wednesday, September 17, 2014

Django datetime localisation by user session

Hello,

I'm trying to localise the date time format in my product so that it appears in the UK format of DD/MM/YYYY HH:MM by default unless the user explicitly changes it to the US format of MM/DD/YYYY HH:MM.

I've got a number of modal forms that contain a `DateTimeField` and I've overridden the format by doing the following:

    class StoreForm(FormBase):
        def __init__(self, *args, **kwargs):
            super(StoreForm, self).__init__(*args, **kwargs)
            #Localise date format
            self.fields["CreationTime"].widget.format = "%m/%d/%Y %H:%M"

Note that the code is drastically simplified as it forces the US date time format. Now the date correctly displays in the US time format, but the form fails to validate on POST. It thinks that the date format is invalid.

I've left the [DATETIME_INPUT_FORMATS](https://docs.djangoproject.com/en/dev/ref/settings/#std:setting-DATETIME_INPUT_FORMATS) variable in `settings.py` untouched (i.e. default for the locale). Perhaps this is the mistake? 

As a general note, I've found the localisation documentation for Django to be quite insufficient so I am totally happy to be pointed in the right direction if my approach here is thoroughly wrong.

Kind regards,
Phil

--
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+unsubscribe@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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/72d918e9-660b-419c-9826-842f61a08ceb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment