Ticket #5917: newforms.extra.widgets.py.diff

File newforms.extra.widgets.py.diff, 1.8 KB (added by Camille Harang <mammique@…>, 17 years ago)

Fix + ID's

  • widgets.py

    old new  
    3030            self.years = range(this_year, this_year+10)
    3131
    3232    def render(self, name, value, attrs=None):
     33
     34        has_id = attrs and 'id' in attrs
     35        final_attrs = self.build_attrs(attrs, name=name)
     36
     37        def select_id(n):
     38            if has_id: return dict(final_attrs, id='%s_%s' % (attrs['id'], n))
     39            else: return None
     40
    3341        try:
    34             value = datetime.date(*map(int, value.split('-')))
     42            if type(value) == type(u''):
     43                value = datetime.date(*map(int, value.split('-')))
    3544            year_val, month_val, day_val = value.year, value.month, value.day
    3645        except (AttributeError, TypeError, ValueError):
    3746            year_val = month_val = day_val = None
     
    4049
    4150        month_choices = MONTHS.items()
    4251        month_choices.sort()
    43         select_html = Select(choices=month_choices).render(self.month_field % name, month_val)
     52        select_html = Select(select_id('month'), choices=month_choices).render(self.month_field % name, month_val)
    4453        output.append(select_html)
    4554
    4655        day_choices = [(i, i) for i in range(1, 32)]
    47         select_html = Select(choices=day_choices).render(self.day_field % name, day_val)
     56        select_html = Select(select_id('day'), choices=day_choices).render(self.day_field % name, day_val)
    4857        output.append(select_html)
    4958
    5059        year_choices = [(i, i) for i in self.years]
    51         select_html = Select(choices=year_choices).render(self.year_field % name, year_val)
     60        select_html = Select(select_id('year'), choices=year_choices).render(self.year_field % name, year_val)
    5261        output.append(select_html)
    5362
    5463        return u'\n'.join(output)
Back to Top