Code

Ticket #4117: widget-ul-id.2.diff

File widget-ul-id.2.diff, 8.3 KB (added by Alex, 6 years ago)

Same patch, just updated to apply against forms

Line 
1diff --git a/django/forms/widgets.py b/django/forms/widgets.py
2index ebbf2ab..37d08e1 100644
3--- a/django/forms/widgets.py
4+++ b/django/forms/widgets.py
5@@ -321,8 +321,12 @@ class RadioFieldRenderer(StrAndUnicode):
6 
7     def render(self):
8         """Outputs a <ul> for this set of radio fields."""
9-        return mark_safe(u'<ul>\n%s\n</ul>' % u'\n'.join([u'<li>%s</li>'
10-                % force_unicode(w) for w in self]))
11+        att = {}
12+        id_ = self.attrs.get('id')
13+        if id_:
14+            att['id'] = id_
15+        return mark_safe(u'<ul%s>\n%s\n</ul>' % (flatatt(att), u'\n'.join([u'<li>%s</li>'
16+                % force_unicode(w) for w in self])))
17 
18 class RadioSelect(Select):
19     renderer = RadioFieldRenderer
20@@ -360,7 +364,11 @@ class CheckboxSelectMultiple(SelectMultiple):
21         if value is None: value = []
22         has_id = attrs and 'id' in attrs
23         final_attrs = self.build_attrs(attrs, name=name)
24-        output = [u'<ul>']
25+        att = {}
26+        id_ = final_attrs.get('id')
27+        if id_:
28+            att['id'] = id_
29+        output = [u'<ul%s>' % flatatt(att)]
30         # Normalize to strings
31         str_values = set([force_unicode(v) for v in value])
32         for i, (option_value, option_label) in enumerate(chain(self.choices, choices)):
33diff --git a/tests/regressiontests/forms/forms.py b/tests/regressiontests/forms/forms.py
34index 7fc206d..1ef290f 100644
35--- a/tests/regressiontests/forms/forms.py
36+++ b/tests/regressiontests/forms/forms.py
37@@ -442,7 +442,7 @@ gets a distinct ID, formed by appending an underscore plus the button's
38 zero-based index.
39 >>> f = FrameworkForm(auto_id='id_%s')
40 >>> print f['language']
41-<ul>
42+<ul id="id_language">
43 <li><label for="id_language_0"><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
44 <li><label for="id_language_1"><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
45 </ul>
46@@ -452,19 +452,19 @@ either as_table() or as_ul(), the label for the RadioSelect will point to the
47 ID of the *first* radio button.
48 >>> print f
49 <tr><th><label for="id_name">Name:</label></th><td><input type="text" name="name" id="id_name" /></td></tr>
50-<tr><th><label for="id_language_0">Language:</label></th><td><ul>
51+<tr><th><label for="id_language_0">Language:</label></th><td><ul id="id_language">
52 <li><label for="id_language_0"><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
53 <li><label for="id_language_1"><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
54 </ul></td></tr>
55 >>> print f.as_ul()
56 <li><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></li>
57-<li><label for="id_language_0">Language:</label> <ul>
58+<li><label for="id_language_0">Language:</label> <ul id="id_language">
59 <li><label for="id_language_0"><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
60 <li><label for="id_language_1"><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
61 </ul></li>
62 >>> print f.as_p()
63 <p><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></p>
64-<p><label for="id_language_0">Language:</label> <ul>
65+<p><label for="id_language_0">Language:</label> <ul id="id_language">
66 <li><label for="id_language_0"><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
67 <li><label for="id_language_1"><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
68 </ul></p>
69@@ -534,7 +534,7 @@ gets a distinct ID, formed by appending an underscore plus the checkbox's
70 zero-based index.
71 >>> f = SongForm(auto_id='%s_id')
72 >>> print f['composers']
73-<ul>
74+<ul id="composers_id">
75 <li><label for="composers_id_0"><input type="checkbox" name="composers" value="J" id="composers_id_0" /> John Lennon</label></li>
76 <li><label for="composers_id_1"><input type="checkbox" name="composers" value="P" id="composers_id_1" /> Paul McCartney</label></li>
77 </ul>
78diff --git a/tests/regressiontests/forms/regressions.py b/tests/regressiontests/forms/regressions.py
79index cbc8095..0f634d7 100644
80--- a/tests/regressiontests/forms/regressions.py
81+++ b/tests/regressiontests/forms/regressions.py
82@@ -40,7 +40,7 @@ Unicode decoding problems...
83 ...     somechoice = ChoiceField(choices=GENDERS, widget=RadioSelect(), label=u'\xc5\xf8\xdf')
84 >>> f = SomeForm()
85 >>> f.as_p()
86-u'<p><label for="id_somechoice_0">\xc5\xf8\xdf:</label> <ul>\n<li><label for="id_somechoice_0"><input type="radio" id="id_somechoice_0" value="\xc5" name="somechoice" /> En tied\xe4</label></li>\n<li><label for="id_somechoice_1"><input type="radio" id="id_somechoice_1" value="\xf8" name="somechoice" /> Mies</label></li>\n<li><label for="id_somechoice_2"><input type="radio" id="id_somechoice_2" value="\xdf" name="somechoice" /> Nainen</label></li>\n</ul></p>'
87+u'<p><label for="id_somechoice_0">\xc5\xf8\xdf:</label> <ul id="id_somechoice">\n<li><label for="id_somechoice_0"><input type="radio" id="id_somechoice_0" value="\xc5" name="somechoice" /> En tied\xe4</label></li>\n<li><label for="id_somechoice_1"><input type="radio" id="id_somechoice_1" value="\xf8" name="somechoice" /> Mies</label></li>\n<li><label for="id_somechoice_2"><input type="radio" id="id_somechoice_2" value="\xdf" name="somechoice" /> Nainen</label></li>\n</ul></p>'
88 
89 Testing choice validation with UTF-8 bytestrings as input (these are the
90 Russian abbreviations "мес." and "шт.".
91@@ -56,7 +56,7 @@ Translated error messages used to be buggy.
92 >>> activate('ru')
93 >>> f = SomeForm({})
94 >>> f.as_p()
95-u'<ul class="errorlist"><li>\u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u043b\u0435.</li></ul>\n<p><label for="id_somechoice_0">\xc5\xf8\xdf:</label> <ul>\n<li><label for="id_somechoice_0"><input type="radio" id="id_somechoice_0" value="\xc5" name="somechoice" /> En tied\xe4</label></li>\n<li><label for="id_somechoice_1"><input type="radio" id="id_somechoice_1" value="\xf8" name="somechoice" /> Mies</label></li>\n<li><label for="id_somechoice_2"><input type="radio" id="id_somechoice_2" value="\xdf" name="somechoice" /> Nainen</label></li>\n</ul></p>'
96+u'<ul class="errorlist"><li>\u041e\u0431\u044f\u0437\u0430\u0442\u0435\u043b\u044c\u043d\u043e\u0435 \u043f\u043e\u043b\u0435.</li></ul>\n<p><label for="id_somechoice_0">\xc5\xf8\xdf:</label> <ul id="id_somechoice">\n<li><label for="id_somechoice_0"><input type="radio" id="id_somechoice_0" value="\xc5" name="somechoice" /> En tied\xe4</label></li>\n<li><label for="id_somechoice_1"><input type="radio" id="id_somechoice_1" value="\xf8" name="somechoice" /> Mies</label></li>\n<li><label for="id_somechoice_2"><input type="radio" id="id_somechoice_2" value="\xdf" name="somechoice" /> Nainen</label></li>\n</ul></p>'
97 >>> deactivate()
98 
99 Deep copying translated text shouldn't raise an error
100diff --git a/tests/regressiontests/forms/widgets.py b/tests/regressiontests/forms/widgets.py
101index 2c6b51a..d0ad268 100644
102--- a/tests/regressiontests/forms/widgets.py
103+++ b/tests/regressiontests/forms/widgets.py
104@@ -740,7 +740,7 @@ u'<ul>\n<li><label><input checked="checked" type="radio" name="email" value="\u0
105 # Attributes provided at instantiation are passed to the constituent inputs
106 >>> w = RadioSelect(attrs={'id':'foo'})
107 >>> print w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
108-<ul>
109+<ul id="foo">
110 <li><label for="foo_0"><input checked="checked" type="radio" id="foo_0" value="J" name="beatle" /> John</label></li>
111 <li><label for="foo_1"><input type="radio" id="foo_1" value="P" name="beatle" /> Paul</label></li>
112 <li><label for="foo_2"><input type="radio" id="foo_2" value="G" name="beatle" /> George</label></li>
113@@ -750,7 +750,7 @@ u'<ul>\n<li><label><input checked="checked" type="radio" name="email" value="\u0
114 # Attributes provided at render-time are passed to the constituent inputs
115 >>> w = RadioSelect()
116 >>> print w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')), attrs={'id':'bar'})
117-<ul>
118+<ul id="bar">
119 <li><label for="bar_0"><input checked="checked" type="radio" id="bar_0" value="J" name="beatle" /> John</label></li>
120 <li><label for="bar_1"><input type="radio" id="bar_1" value="P" name="beatle" /> Paul</label></li>
121 <li><label for="bar_2"><input type="radio" id="bar_2" value="G" name="beatle" /> George</label></li>