diff --git a/django/newforms/extras/widgets.py b/django/newforms/extras/widgets.py index 0097ba3f54..af6b14b08d 100644 --- a/django/newforms/extras/widgets.py +++ b/django/newforms/extras/widgets.py @@ -39,21 +39,33 @@ class SelectDateWidget(Widget): output = [] + if 'id' in self.attrs: + id_ = self.attrs['id'] + else: + id_ = 'id_%s' % name + month_choices = MONTHS.items() month_choices.sort() - select_html = Select(choices=month_choices).render(self.month_field % name, month_val) + 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) output.append(select_html) day_choices = [(i, i) for i in range(1, 32)] - select_html = Select(choices=day_choices).render(self.day_field % name, day_val) + local_attrs['id'] = self.day_field % id_ + select_html = Select(choices=day_choices).render(self.day_field % name, day_val, local_attrs) output.append(select_html) year_choices = [(i, i) for i in self.years] - select_html = Select(choices=year_choices).render(self.year_field % name, year_val) + local_attrs['id'] = self.year_field % id_ + select_html = Select(choices=year_choices).render(self.year_field % name, year_val, local_attrs) output.append(select_html) return mark_safe(u'\n'.join(output)) + def id_for_label(self, id_): + return '%s_month' % id_ + id_for_label = classmethod(id_for_label) + 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) if y and m and d: diff --git a/tests/regressiontests/forms/extra.py b/tests/regressiontests/forms/extra.py index 9dff4071f1..0512e96c06 100644 --- a/tests/regressiontests/forms/extra.py +++ b/tests/regressiontests/forms/extra.py @@ -22,7 +22,7 @@ classes that demonstrate some of the library's abilities. >>> from django.newforms.extras import SelectDateWidget >>> w = SelectDateWidget(years=('2007','2008','2009','2010','2011','2012','2013','2014','2015','2016')) >>> print w.render('mydate', '') - @@ -36,7 +36,7 @@ classes that demonstrate some of the library's abilities. - @@ -69,7 +69,7 @@ classes that demonstrate some of the library's abilities. - @@ -84,7 +84,7 @@ classes that demonstrate some of the library's abilities. >>> w.render('mydate', None) == w.render('mydate', '') True >>> print w.render('mydate', '2010-04-15') - @@ -98,7 +98,7 @@ True - @@ -131,7 +131,7 @@ True -