From 5926a26e920c0543641ea1b06ee78c61c80e947d Mon Sep 17 00:00:00 2001 From: Russell Keith-Magee Date: Wed, 28 Apr 2010 15:39:26 +0000 Subject: [PATCH] Fixed #12986 -- Ensured that SelectDateField repopulates correctly when USE_L10N=True and locale has a default date input format other than %Y-%m-%d. Thanks to wim@go2people.nl for the report, and walteralini for his draft patch. git-svn-id: http://code.djangoproject.com/svn/django/trunk@13041 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/forms/extras/widgets.py | 17 +++++-- tests/regressiontests/forms/extra.py | 76 ++++++++++++++++++++++++++++ 2 files changed, 88 insertions(+), 5 deletions(-) diff --git a/django/forms/extras/widgets.py b/django/forms/extras/widgets.py index 173ef2e1de..108b2c56e2 100644 --- a/django/forms/extras/widgets.py +++ b/django/forms/extras/widgets.py @@ -43,10 +43,17 @@ class SelectDateWidget(Widget): except AttributeError: year_val = month_val = day_val = None if isinstance(value, basestring): - match = RE_DATE.match(value) - if match: - year_val, month_val, day_val = [int(v) for v in match.groups()] - + if settings.USE_L10N: + try: + input_format = get_format('DATE_INPUT_FORMATS')[0] + v = datetime.datetime.strptime(value, input_format) + year_val, month_val, day_val = v.year, v.month, v.day + except ValueError: + pass + else: + match = RE_DATE.match(value) + if match: + year_val, month_val, day_val = [int(v) for v in match.groups()] choices = [(i, i) for i in self.years] year_html = self.create_select(name, self.year_field, value, year_val, choices) choices = MONTHS.items() @@ -98,7 +105,7 @@ class SelectDateWidget(Widget): id_ = self.attrs['id'] else: id_ = 'id_%s' % name - if not (self.required and value): + if not (self.required and val): choices.insert(0, self.none_value) local_attrs = self.build_attrs(id=field % id_) s = Select(choices=choices) diff --git a/tests/regressiontests/forms/extra.py b/tests/regressiontests/forms/extra.py index eb586d0ecc..2d25530a48 100644 --- a/tests/regressiontests/forms/extra.py +++ b/tests/regressiontests/forms/extra.py @@ -364,6 +364,82 @@ True 2008-04-01 +USE_L10N tests + +>>> from django.utils import translation +>>> translation.activate('nl') +>>> from django.conf import settings +>>> settings.USE_L10N=True + +>>> w.value_from_datadict({'date_year': '2010', 'date_month': '8', 'date_day': '13'}, {}, 'date') +'13-08-2010' + +>>> print w.render('date', '13-08-2010') + + + + +>>> translation.deactivate() + # MultiWidget and MultiValueField ############################################# # MultiWidgets are widgets composed of other widgets. They are usually # combined with MultiValueFields - a field that is composed of other fields.