Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #12986 -- Ensured that SelectDateField repopulates correctly wh…

…en 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
  • Loading branch information...
commit 5926a26e920c0543641ea1b06ee78c61c80e947d 1 parent 9d8492c
Russell Keith-Magee authored April 28, 2010
17  django/forms/extras/widgets.py
@@ -43,10 +43,17 @@ def render(self, name, value, attrs=None):
43 43
         except AttributeError:
44 44
             year_val = month_val = day_val = None
45 45
             if isinstance(value, basestring):
46  
-                match = RE_DATE.match(value)
47  
-                if match:
48  
-                    year_val, month_val, day_val = [int(v) for v in match.groups()]
49  
-
  46
+                if settings.USE_L10N:
  47
+                    try:
  48
+                        input_format = get_format('DATE_INPUT_FORMATS')[0]
  49
+                        v = datetime.datetime.strptime(value, input_format)
  50
+                        year_val, month_val, day_val = v.year, v.month, v.day
  51
+                    except ValueError:
  52
+                        pass
  53
+                else:
  54
+                    match = RE_DATE.match(value)
  55
+                    if match:
  56
+                        year_val, month_val, day_val = [int(v) for v in match.groups()]
50 57
         choices = [(i, i) for i in self.years]
51 58
         year_html = self.create_select(name, self.year_field, value, year_val, choices)
52 59
         choices = MONTHS.items()
@@ -98,7 +105,7 @@ def create_select(self, name, field, value, val, choices):
98 105
             id_ = self.attrs['id']
99 106
         else:
100 107
             id_ = 'id_%s' % name
101  
-        if not (self.required and value):
  108
+        if not (self.required and val):
102 109
             choices.insert(0, self.none_value)
103 110
         local_attrs = self.build_attrs(id=field % id_)
104 111
         s = Select(choices=choices)
76  tests/regressiontests/forms/extra.py
@@ -364,6 +364,82 @@
364 364
 2008-04-01
365 365
 
366 366
 
  367
+USE_L10N tests
  368
+
  369
+>>> from django.utils import  translation
  370
+>>> translation.activate('nl')
  371
+>>> from django.conf import settings
  372
+>>> settings.USE_L10N=True
  373
+
  374
+>>> w.value_from_datadict({'date_year': '2010', 'date_month': '8', 'date_day': '13'}, {}, 'date')
  375
+'13-08-2010'
  376
+
  377
+>>> print w.render('date', '13-08-2010')
  378
+<select name="date_day" id="id_date_day">
  379
+<option value="0">---</option>
  380
+<option value="1">1</option>
  381
+<option value="2">2</option>
  382
+<option value="3">3</option>
  383
+<option value="4">4</option>
  384
+<option value="5">5</option>
  385
+<option value="6">6</option>
  386
+<option value="7">7</option>
  387
+<option value="8">8</option>
  388
+<option value="9">9</option>
  389
+<option value="10">10</option>
  390
+<option value="11">11</option>
  391
+<option value="12">12</option>
  392
+<option value="13" selected="selected">13</option>
  393
+<option value="14">14</option>
  394
+<option value="15">15</option>
  395
+<option value="16">16</option>
  396
+<option value="17">17</option>
  397
+<option value="18">18</option>
  398
+<option value="19">19</option>
  399
+<option value="20">20</option>
  400
+<option value="21">21</option>
  401
+<option value="22">22</option>
  402
+<option value="23">23</option>
  403
+<option value="24">24</option>
  404
+<option value="25">25</option>
  405
+<option value="26">26</option>
  406
+<option value="27">27</option>
  407
+<option value="28">28</option>
  408
+<option value="29">29</option>
  409
+<option value="30">30</option>
  410
+<option value="31">31</option>
  411
+</select>
  412
+<select name="date_month" id="id_date_month">
  413
+<option value="0">---</option>
  414
+<option value="1">januari</option>
  415
+<option value="2">februari</option>
  416
+<option value="3">maart</option>
  417
+<option value="4">april</option>
  418
+<option value="5">mei</option>
  419
+<option value="6">juni</option>
  420
+<option value="7">juli</option>
  421
+<option value="8" selected="selected">augustus</option>
  422
+<option value="9">september</option>
  423
+<option value="10">oktober</option>
  424
+<option value="11">november</option>
  425
+<option value="12">december</option>
  426
+</select>
  427
+<select name="date_year" id="id_date_year">
  428
+<option value="0">---</option>
  429
+<option value="2007">2007</option>
  430
+<option value="2008">2008</option>
  431
+<option value="2009">2009</option>
  432
+<option value="2010" selected="selected">2010</option>
  433
+<option value="2011">2011</option>
  434
+<option value="2012">2012</option>
  435
+<option value="2013">2013</option>
  436
+<option value="2014">2014</option>
  437
+<option value="2015">2015</option>
  438
+<option value="2016">2016</option>
  439
+</select>
  440
+
  441
+>>> translation.deactivate()
  442
+
367 443
 # MultiWidget and MultiValueField #############################################
368 444
 # MultiWidgets are widgets composed of other widgets. They are usually
369 445
 # combined with MultiValueFields - a field that is composed of other fields.

0 notes on commit 5926a26

Please sign in to comment.
Something went wrong with that request. Please try again.