Code

Ticket #11843: 11843.diff

File 11843.diff, 1.2 KB (added by SmileyChris, 5 years ago)
Line 
1Index: django/forms/widgets.py
2===================================================================
3--- django/forms/widgets.py     (revision 11378)
4+++ django/forms/widgets.py     (working copy)
5@@ -247,9 +247,16 @@
6     def render(self, name, value, attrs=None, choices=()):
7         if value is None: value = []
8         final_attrs = self.build_attrs(attrs, type=self.input_type, name=name)
9-        return mark_safe(u'\n'.join([(u'<input%s />' %
10-            flatatt(dict(value=force_unicode(v), **final_attrs)))
11-            for v in value]))
12+        id_attr = final_attrs.get('id')
13+        inputs = []
14+        for i, v in enumerate(value):
15+            input_attrs = dict(value=force_unicode(v), **final_attrs)
16+            if id_attr:
17+                # An ID attribute was given. Add a numeric index as a suffix
18+                # so that the inputs don't all have the same ID attribute.
19+                input_attrs['id'] = '%s_%s' % (id_attr, i)
20+            inputs.append(u'<input%s />' % flatatt(input_attrs))
21+        return mark_safe(u'\n'.join(inputs))
22 
23     def value_from_datadict(self, data, files, name):
24         if isinstance(data, (MultiValueDict, MergeDict)):