diff --git a/django/forms/widgets.py b/django/forms/widgets.py
index 1fbef98..910916b 100644
a
|
b
|
class MultiWidget(Widget):
|
795 | 795 | """ |
796 | 796 | def __init__(self, widgets, attrs=None): |
797 | 797 | self.widgets = [isinstance(w, type) and w() or w for w in widgets] |
798 | | super(MultiWidget, self).__init__(attrs) |
| 798 | if attrs is not None: |
| 799 | self.attrs = copy.deepcopy(attrs) |
| 800 | else: |
| 801 | self.attrs = {} |
799 | 802 | |
800 | 803 | def render(self, name, value, attrs=None): |
801 | 804 | if self.is_localized: |
… |
… |
class MultiWidget(Widget):
|
806 | 809 | if not isinstance(value, list): |
807 | 810 | value = self.decompress(value) |
808 | 811 | output = [] |
809 | | final_attrs = self.build_attrs(attrs) |
810 | | id_ = final_attrs.get('id', None) |
811 | 812 | for i, widget in enumerate(self.widgets): |
812 | 813 | try: |
813 | 814 | widget_value = value[i] |
814 | 815 | except IndexError: |
815 | 816 | widget_value = None |
| 817 | if isinstance(self.attrs, list): |
| 818 | try: |
| 819 | final_attrs= dict(self.attrs[i]) |
| 820 | except IndexError: |
| 821 | final_attrs = {} |
| 822 | else: |
| 823 | final_attrs = dict(self.attrs) |
| 824 | if attrs is not None: |
| 825 | if isinstance(attrs, list): |
| 826 | try: |
| 827 | final_attrs.update(attrs[i]) |
| 828 | except IndexError: |
| 829 | pass |
| 830 | else: |
| 831 | final_attrs.update(attrs) |
| 832 | id_ = final_attrs.get('id', None) |
816 | 833 | if id_: |
817 | 834 | final_attrs = dict(final_attrs, id='%s_%s' % (id_, i)) |
818 | 835 | output.append(widget.render(name + '_%s' % i, widget_value, final_attrs)) |
… |
… |
class SplitDateTimeWidget(MultiWidget):
|
875 | 892 | """ |
876 | 893 | |
877 | 894 | def __init__(self, attrs=None, date_format=None, time_format=None): |
878 | | widgets = (DateInput(attrs=attrs, format=date_format), |
879 | | TimeInput(attrs=attrs, format=time_format)) |
| 895 | widgets = (DateInput(format=date_format), |
| 896 | TimeInput(format=time_format)) |
880 | 897 | super(SplitDateTimeWidget, self).__init__(widgets, attrs) |
881 | 898 | |
882 | 899 | def decompress(self, value): |
diff --git a/tests/regressiontests/forms/tests/widgets.py b/tests/regressiontests/forms/tests/widgets.py
index 2499b7a..edeccad 100644
a
|
b
|
beatle J R Ringo False""")
|
870 | 870 | w = MyMultiWidget(widgets=(TextInput(attrs={'class': 'big'}), TextInput(attrs={'class': 'small'})), attrs={'id': 'bar'}) |
871 | 871 | self.assertHTMLEqual(w.render('name', ['john', 'lennon']), u'<input id="bar_0" type="text" class="big" value="john" name="name_0" /><br /><input id="bar_1" type="text" class="small" value="lennon" name="name_1" />') |
872 | 872 | |
| 873 | # test passing list of attrs, each widget gets the right attrs |
| 874 | w = MyMultiWidget(widgets=(TextInput, TextInput),attrs=[{'class': 'big'}, {'class': 'small'}]) |
| 875 | self.assertHTMLEqual(w.render('name', ['john', 'lennon']), u'<input type="text" class="big" value="john" name="name_0" /><br /><input type="text" class="small" value="lennon" name="name_1" />') |
| 876 | |
| 877 | # test incomplete list of attrs |
| 878 | w = MyMultiWidget(widgets=(TextInput, TextInput),attrs=[{'class': 'big'},]) |
| 879 | self.assertHTMLEqual(w.render('name', ['john', 'lennon']), u'<input type="text" class="big" value="john" name="name_0" /><br /><input type="text" value="lennon" name="name_1" />') |
| 880 | |
873 | 881 | w = MyMultiWidget(widgets=(TextInput(), TextInput())) |
874 | 882 | |
875 | 883 | # test with no initial data |
… |
… |
beatle J R Ringo False""")
|
896 | 904 | w = SplitDateTimeWidget(attrs={'class': 'pretty'}) |
897 | 905 | self.assertHTMLEqual(w.render('date', datetime.datetime(2006, 1, 10, 7, 30)), u'<input type="text" class="pretty" value="2006-01-10" name="date_0" /><input type="text" class="pretty" value="07:30:00" name="date_1" />') |
898 | 906 | |
| 907 | # different attrs for the subwidgets |
| 908 | w = SplitDateTimeWidget(attrs=[{'class': 'pretty'}, {'class': 'ugly'}]) |
| 909 | self.assertHTMLEqual(w.render('date', datetime.datetime(2006, 1, 10, 7, 30)), u'<input type="text" class="pretty" value="2006-01-10" name="date_0" /><input type="text" class="ugly" value="07:30:00" name="date_1" />') |
| 910 | |
899 | 911 | # Use 'date_format' and 'time_format' to change the way a value is displayed. |
900 | 912 | w = SplitDateTimeWidget(date_format='%d/%m/%Y', time_format='%H:%M') |
901 | 913 | self.assertHTMLEqual(w.render('date', datetime.datetime(2006, 1, 10, 7, 30)), u'<input type="text" name="date_0" value="10/01/2006" /><input type="text" name="date_1" value="07:30" />') |