mirror of https://github.com/django/django.git
Fixed #9124: fixed `SelectDateWidget` with `required=False`. Thanks, Bernd Schlapsi.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@10584 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
41260fb931
commit
002e3fd9e4
|
@ -20,13 +20,15 @@ class SelectDateWidget(Widget):
|
||||||
This also serves as an example of a Widget that has more than one HTML
|
This also serves as an example of a Widget that has more than one HTML
|
||||||
element and hence implements value_from_datadict.
|
element and hence implements value_from_datadict.
|
||||||
"""
|
"""
|
||||||
|
none_value = (0, '---')
|
||||||
month_field = '%s_month'
|
month_field = '%s_month'
|
||||||
day_field = '%s_day'
|
day_field = '%s_day'
|
||||||
year_field = '%s_year'
|
year_field = '%s_year'
|
||||||
|
|
||||||
def __init__(self, attrs=None, years=None):
|
def __init__(self, attrs=None, years=None, required=True):
|
||||||
# years is an optional list/tuple of years to use in the "year" select box.
|
# years is an optional list/tuple of years to use in the "year" select box.
|
||||||
self.attrs = attrs or {}
|
self.attrs = attrs or {}
|
||||||
|
self.required = required
|
||||||
if years:
|
if years:
|
||||||
self.years = years
|
self.years = years
|
||||||
else:
|
else:
|
||||||
|
@ -51,19 +53,28 @@ class SelectDateWidget(Widget):
|
||||||
id_ = 'id_%s' % name
|
id_ = 'id_%s' % name
|
||||||
|
|
||||||
month_choices = MONTHS.items()
|
month_choices = MONTHS.items()
|
||||||
|
if not (self.required and value):
|
||||||
|
month_choices.append(self.none_value)
|
||||||
month_choices.sort()
|
month_choices.sort()
|
||||||
local_attrs = self.build_attrs(id=self.month_field % id_)
|
local_attrs = self.build_attrs(id=self.month_field % id_)
|
||||||
select_html = Select(choices=month_choices).render(self.month_field % name, month_val, local_attrs)
|
s = Select(choices=month_choices)
|
||||||
|
select_html = s.render(self.month_field % name, month_val, local_attrs)
|
||||||
output.append(select_html)
|
output.append(select_html)
|
||||||
|
|
||||||
day_choices = [(i, i) for i in range(1, 32)]
|
day_choices = [(i, i) for i in range(1, 32)]
|
||||||
|
if not (self.required and value):
|
||||||
|
day_choices.insert(0, self.none_value)
|
||||||
local_attrs['id'] = self.day_field % id_
|
local_attrs['id'] = self.day_field % id_
|
||||||
select_html = Select(choices=day_choices).render(self.day_field % name, day_val, local_attrs)
|
s = Select(choices=day_choices)
|
||||||
|
select_html = s.render(self.day_field % name, day_val, local_attrs)
|
||||||
output.append(select_html)
|
output.append(select_html)
|
||||||
|
|
||||||
year_choices = [(i, i) for i in self.years]
|
year_choices = [(i, i) for i in self.years]
|
||||||
|
if not (self.required and value):
|
||||||
|
year_choices.insert(0, self.none_value)
|
||||||
local_attrs['id'] = self.year_field % id_
|
local_attrs['id'] = self.year_field % id_
|
||||||
select_html = Select(choices=year_choices).render(self.year_field % name, year_val, local_attrs)
|
s = Select(choices=year_choices)
|
||||||
|
select_html = s.render(self.year_field % name, year_val, local_attrs)
|
||||||
output.append(select_html)
|
output.append(select_html)
|
||||||
|
|
||||||
return mark_safe(u'\n'.join(output))
|
return mark_safe(u'\n'.join(output))
|
||||||
|
@ -73,7 +84,11 @@ class SelectDateWidget(Widget):
|
||||||
id_for_label = classmethod(id_for_label)
|
id_for_label = classmethod(id_for_label)
|
||||||
|
|
||||||
def value_from_datadict(self, data, files, name):
|
def value_from_datadict(self, data, files, name):
|
||||||
y, m, d = data.get(self.year_field % name), data.get(self.month_field % name), data.get(self.day_field % name)
|
y = data.get(self.year_field % name)
|
||||||
|
m = data.get(self.month_field % name)
|
||||||
|
d = data.get(self.day_field % name)
|
||||||
|
if y == m == d == "0":
|
||||||
|
return None
|
||||||
if y and m and d:
|
if y and m and d:
|
||||||
return '%s-%s-%s' % (y, m, d)
|
return '%s-%s-%s' % (y, m, d)
|
||||||
return data.get(name, None)
|
return data.get(name, None)
|
||||||
|
|
|
@ -23,6 +23,7 @@ classes that demonstrate some of the library's abilities.
|
||||||
>>> w = SelectDateWidget(years=('2007','2008','2009','2010','2011','2012','2013','2014','2015','2016'))
|
>>> w = SelectDateWidget(years=('2007','2008','2009','2010','2011','2012','2013','2014','2015','2016'))
|
||||||
>>> print w.render('mydate', '')
|
>>> print w.render('mydate', '')
|
||||||
<select name="mydate_month" id="id_mydate_month">
|
<select name="mydate_month" id="id_mydate_month">
|
||||||
|
<option value="0">---</option>
|
||||||
<option value="1">January</option>
|
<option value="1">January</option>
|
||||||
<option value="2">February</option>
|
<option value="2">February</option>
|
||||||
<option value="3">March</option>
|
<option value="3">March</option>
|
||||||
|
@ -37,6 +38,7 @@ classes that demonstrate some of the library's abilities.
|
||||||
<option value="12">December</option>
|
<option value="12">December</option>
|
||||||
</select>
|
</select>
|
||||||
<select name="mydate_day" id="id_mydate_day">
|
<select name="mydate_day" id="id_mydate_day">
|
||||||
|
<option value="0">---</option>
|
||||||
<option value="1">1</option>
|
<option value="1">1</option>
|
||||||
<option value="2">2</option>
|
<option value="2">2</option>
|
||||||
<option value="3">3</option>
|
<option value="3">3</option>
|
||||||
|
@ -70,6 +72,7 @@ classes that demonstrate some of the library's abilities.
|
||||||
<option value="31">31</option>
|
<option value="31">31</option>
|
||||||
</select>
|
</select>
|
||||||
<select name="mydate_year" id="id_mydate_year">
|
<select name="mydate_year" id="id_mydate_year">
|
||||||
|
<option value="0">---</option>
|
||||||
<option value="2007">2007</option>
|
<option value="2007">2007</option>
|
||||||
<option value="2008">2008</option>
|
<option value="2008">2008</option>
|
||||||
<option value="2009">2009</option>
|
<option value="2009">2009</option>
|
||||||
|
@ -213,6 +216,133 @@ Invalid dates still render the failed date:
|
||||||
|
|
||||||
Using a SelectDateWidget in a form:
|
Using a SelectDateWidget in a form:
|
||||||
|
|
||||||
|
>>> w = SelectDateWidget(years=('2007','2008','2009','2010','2011','2012','2013','2014','2015','2016'), required=False)
|
||||||
|
>>> print w.render('mydate', '')
|
||||||
|
<select name="mydate_month" id="id_mydate_month">
|
||||||
|
<option value="0">---</option>
|
||||||
|
<option value="1">January</option>
|
||||||
|
<option value="2">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>
|
||||||
|
<select name="mydate_day" id="id_mydate_day">
|
||||||
|
<option value="0">---</option>
|
||||||
|
<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">31</option>
|
||||||
|
</select>
|
||||||
|
<select name="mydate_year" id="id_mydate_year">
|
||||||
|
<option value="0">---</option>
|
||||||
|
<option value="2007">2007</option>
|
||||||
|
<option value="2008">2008</option>
|
||||||
|
<option value="2009">2009</option>
|
||||||
|
<option value="2010">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>
|
||||||
|
</select>
|
||||||
|
>>> print w.render('mydate', '2010-04-15')
|
||||||
|
<select name="mydate_month" id="id_mydate_month">
|
||||||
|
<option value="0">---</option>
|
||||||
|
<option value="1">January</option>
|
||||||
|
<option value="2">February</option>
|
||||||
|
<option value="3">March</option>
|
||||||
|
<option value="4" selected="selected">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>
|
||||||
|
<select name="mydate_day" id="id_mydate_day">
|
||||||
|
<option value="0">---</option>
|
||||||
|
<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" selected="selected">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">31</option>
|
||||||
|
</select>
|
||||||
|
<select name="mydate_year" id="id_mydate_year">
|
||||||
|
<option value="0">---</option>
|
||||||
|
<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>
|
||||||
|
</select>
|
||||||
>>> class GetDate(Form):
|
>>> class GetDate(Form):
|
||||||
... mydate = DateField(widget=SelectDateWidget)
|
... mydate = DateField(widget=SelectDateWidget)
|
||||||
>>> a = GetDate({'mydate_month':'4', 'mydate_day':'1', 'mydate_year':'2008'})
|
>>> a = GetDate({'mydate_month':'4', 'mydate_day':'1', 'mydate_year':'2008'})
|
||||||
|
|
Loading…
Reference in New Issue