views:

259

answers:

1

I have an application in Django 1.2. Language is selectable (I18N and Locale = True)

When I select the english lang. in the site, the admin works OK. But when I change to any other language this is what happens with date inputs (spanish example):

Correctly, the input accepts the spanish format %d/%m/%Y (Even selecting from the calendar, the date inserts as expected). But when I save the form and load it again, the date shows in the english form: %Y-%m-%d

The real problem is that when I load the form to change any other text field and try to save it I get an error telling me to enter a valid date, so I have to write all dates again or change the language in the site to use the admin.

I haven't specified anything for DATE_INPUT_FORMATS in settings nor have I overridden forms or models.

Surely I am missing something but I can't find it. Can anybody give me a hint?

+1  A: 

Adding this to your settings should solve the part you call "the real problem":

DATE_INPUT_FORMATS = (   
    '%d/%m/%Y', '%d/%m/%y',     # '25/10/2006', '25/10/06'
    '%Y-%m-%d', '%y-%m-%d',     # '2006-10-25', '06-10-25'
)

DATETIME_INPUT_FORMATS = (
    '%d/%m/%Y %H:%M:%S',    # '25/10/2006 14:30:59'
    '%d/%m/%Y %H:%M',       # '25/10/2006 14:30'
    '%d/%m/%y %H:%M:%S',    # '25/10/06 14:30:59'
    '%d/%m/%y %H:%M',       # '25/10/06 14:30'
    '%Y-%m-%d %H:%M:%S',    # '2006-10-25 14:30:59'
    '%Y-%m-%d %H:%M',       # '2006-10-25 14:30'
    '%Y-%m-%d',             # '2006-10-25'
)

But it's a problem with Django. I opened a ticket about the issue, but you should comment, because your example shows it is even more serious problem then I thought it was (because as it turned out not all localization accepts both "universal" and "localized" date input formats).

Update: I forgot to add that you can pass localize=True to your date widgets, and they are supposed to then always display dates in localized format. There are some examples of how to do this in this bug report.

I just posted a message about the issue to django-developers mailing list.

Ludwik Trammer
Thanks Ludwik I've followed your links and now I have a better understanding of the problem. You have saved me a lot of time looking for my own mistakes. I added the formats to settings but I'm still getting validation error. Maybe I'm missing something else but I won't be able to check until Monday. Will comment again then. Thank you.
equalium
There is a patch claiming to correct this issue: http://code.djangoproject.com/ticket/13621 You can try applying it and then comment in the bug, saying whether it worked for you. It will help in the development process. BTW, if you find my answer to be helpful you can vote it up, or even accept it.
Ludwik Trammer
As a work-around, what would happen if you just ran the date formats through the translator? For example: `DATE_FORMAT = _('m-d-Y')` ? Would this actually work?
Jack M.
DATE_INPUT_FORMATS is doing nothing, even if I only allow `'%Y-%m-%d', '%y-%m-%d'` when I save, it expects the locale version and validation fails. Cannot confirm the patch of ticket 13621 as I'm getting all the hunks failed and I don't know what's wrong (I had never applied a patch before).
equalium