Django datetime localisation by user session

79 views
Skip to first unread message

Philip Goh

unread,
Sep 17, 2014, 7:45:25 AM9/17/14
to django...@googlegroups.com
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

Collin Anderson

unread,
Sep 17, 2014, 11:20:09 AM9/17/14
to django...@googlegroups.com
Yes, I'd use the *_INPUT_FORMATS settings for this. The first format in the list is how it will be displayed in the admin, though anything in the list is acceptable. You _may_ need to turn off USE_L10N. not sure.
Reply all
Reply to author
Forward
0 new messages