Django DateTimeInput determines language/locale at startup time but this may change later, resulting in validation errors
|Reported by:||django-locale-issue@…||Owned by:||claudep|
|Severity:||Normal||Keywords:||forms, widgets, locale|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||no||Patch needs improvement:||no|
My application works in two locales, 'nl' and 'en'. In some situations, when editing content, Django formats datetimes in the wrong format which won't validate later.
The problem, I suspect is in the fact that DateTimeInput determines/stores the locale format in its init, which means at startup/import time in most cases. It cannot handle a changing locale later.
This is with Django 1.4.x but the relevant code doesn't appear to be any different on 1.5.x
I've tested/verified the issue in an interactive django shell session:
# In my settings I have LANGUAGE_CODE = 'nl' # but the user can switch to english later. # Forms, fields and widgets are usually initialized at startup/import time # DateTimeField users DateTimeInput which uses the current language to determine the preferred format In : from django.contrib.auth.forms import UserChangeForm In : print UserChangeForm().fields['last_login'].widget.format %d-%m-%Y %H:%M:%S # this is a rather dutch format # imagine the user switches to english In : from django.utils import translation In : translation.activate('en') In : print UserChangeForm().fields['last_login'].widget.format %d-%m-%Y %H:%M:%S # the format remains unchanged, which isn't good but not fatal. Let's format a string using this In : from datetime import datetime In : now = datetime.now() In : print UserChangeForm().fields['last_login'].widget._format_value(now) 26-09-2013 15:33:13 # now feed this string back to the field, as if the form was filled in and then submitted without change In : print UserChangeForm().fields['last_login'].to_python("26-09-2013 15:33:13") --------------------------------------------------------------------------- ValidationError Traceback (most recent call last) /home/ivo/.buildout/eggs/Django-1.4.5-py2.7.egg/django/core/management/commands/shell.pyc in <module>() ----> 1 print UserChangeForm().fields['last_login'].to_python("26-09-2013 15:33:13") /home/ivo/.buildout/eggs/Django-1.4.5-py2.7.egg/django/forms/fields.pyc in to_python(self, value) 435 return None 436 value = '%s %s' % tuple(value) --> 437 result = super(DateTimeField, self).to_python(value) 438 return from_current_timezone(result) 439 /home/ivo/.buildout/eggs/Django-1.4.5-py2.7.egg/django/forms/fields.pyc in to_python(self, value) 354 except ValueError: 355 continue --> 356 raise ValidationError(self.error_messages['invalid']) 357 358 def strptime(self, value, format): ValidationError: [u'Enter a valid date/time.'] # oops!
Change History (9)
comment:1 Changed 21 months ago by EvilDMP
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
- Triage Stage changed from Unreviewed to Accepted
comment:5 Changed 21 months ago by claudep
- Owner changed from nobody to claudep
- Status changed from new to assigned
- Version changed from 1.4 to master
comment:7 Changed 21 months ago by Claude Paroz <claude@…>
- Resolution set to fixed
- Status changed from assigned to closed