Browse files

Fixed #5917 -- More error robustness in date parsing in SelectDateWid…

…get, plus

keep the original date selected on redisplay, even if it was bogus (e.g. 31
Feb). Patch from SmileyChris.

git-svn-id: bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
1 parent 516102d commit 675db84f2303650140f867b31dbac9b7edf71eb3 @malcolmt malcolmt committed Mar 20, 2008
Showing with 75 additions and 2 deletions.
  1. +8 −2 django/newforms/extras/
  2. +67 −0 tests/regressiontests/forms/
10 django/newforms/extras/
@@ -3,13 +3,16 @@
import datetime
+import re
from django.newforms.widgets import Widget, Select
from django.utils.dates import MONTHS
from django.utils.safestring import mark_safe
__all__ = ('SelectDateWidget',)
+RE_DATE = re.compile(r'(\d{4})-(\d\d?)-(\d\d?)$')
class SelectDateWidget(Widget):
A Widget that splits date input into three <select> boxes.
@@ -32,10 +35,13 @@ def __init__(self, attrs=None, years=None):
def render(self, name, value, attrs=None):
- value =*map(int, value.split('-')))
year_val, month_val, day_val = value.year, value.month,
- except (AttributeError, TypeError, ValueError):
+ 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()]
output = []
67 tests/regressiontests/forms/
@@ -144,6 +144,73 @@
<option value="2016">2016</option>
+Accepts a datetime or a string:
+>>> w.render('mydate',, 4, 15)) == w.render('mydate', '2010-04-15')
+Invalid dates still render the failed date:
+>>> print w.render('mydate', '2010-02-31')
+<select name="mydate_month" id="id_mydate_month">
+<option value="1">January</option>
+<option value="2" selected="selected">February</option>
+<option value="3">March</option>
+<option value="4">April</option>
+<option value="5">May</option>
+<option value="6">June</option>
+<option value="7">July</option>
+<option value="8">August</option>
+<option value="9">September</option>
+<option value="10">October</option>
+<option value="11">November</option>
+<option value="12">December</option>
+<select name="mydate_day" id="id_mydate_day">
+<option value="1">1</option>
+<option value="2">2</option>
+<option value="3">3</option>
+<option value="4">4</option>
+<option value="5">5</option>
+<option value="6">6</option>
+<option value="7">7</option>
+<option value="8">8</option>
+<option value="9">9</option>
+<option value="10">10</option>
+<option value="11">11</option>
+<option value="12">12</option>
+<option value="13">13</option>
+<option value="14">14</option>
+<option value="15">15</option>
+<option value="16">16</option>
+<option value="17">17</option>
+<option value="18">18</option>
+<option value="19">19</option>
+<option value="20">20</option>
+<option value="21">21</option>
+<option value="22">22</option>
+<option value="23">23</option>
+<option value="24">24</option>
+<option value="25">25</option>
+<option value="26">26</option>
+<option value="27">27</option>
+<option value="28">28</option>
+<option value="29">29</option>
+<option value="30">30</option>
+<option value="31" selected="selected">31</option>
+<select name="mydate_year" id="id_mydate_year">
+<option value="2007">2007</option>
+<option value="2008">2008</option>
+<option value="2009">2009</option>
+<option value="2010" selected="selected">2010</option>
+<option value="2011">2011</option>
+<option value="2012">2012</option>
+<option value="2013">2013</option>
+<option value="2014">2014</option>
+<option value="2015">2015</option>
+<option value="2016">2016</option>
Using a SelectDateWidget in a form:
>>> class GetDate(Form):

0 comments on commit 675db84

Please sign in to comment.