Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #4117: Apply id attribute to the outer <ul> of RadioSelect

  • Loading branch information...
commit c4186c2fec6f5418c81366a911792bf5295db494 1 parent f56b703
Baptiste Mispelon authored April 12, 2013 claudep committed April 13, 2013
11  django/forms/widgets.py
@@ -646,10 +646,13 @@ def __str__(self):
646 646
 
647 647
     def render(self):
648 648
         """Outputs a <ul> for this set of radio fields."""
649  
-        return format_html('<ul>\n{0}\n</ul>',
650  
-                           format_html_join('\n', '<li>{0}</li>',
651  
-                                            [(force_text(w),) for w in self]
652  
-                                            ))
  649
+        id_ = self.attrs.get('id', None)
  650
+        start_tag = format_html('<ul id="{0}">', id_) if id_ else '<ul>'
  651
+        output = [start_tag]
  652
+        for widget in self:
  653
+            output.append(format_html('<li>{0}</li>', force_text(widget)))
  654
+        output.append('</ul>')
  655
+        return mark_safe('\n'.join(output))
653 656
 
654 657
 class RadioSelect(Select):
655 658
     renderer = RadioFieldRenderer
5  docs/ref/forms/widgets.txt
@@ -633,6 +633,11 @@ Selector and checkbox widgets
633 633
     If you decide not to loop over the radio buttons -- e.g., if your template simply includes
634 634
     ``{{ myform.beatles }}`` -- they'll be output in a ``<ul>`` with ``<li>`` tags, as above.
635 635
 
  636
+.. versionchanged:: 1.6
  637
+
  638
+The outer ``<ul>`` container will now receive the ``id`` attribute defined on
  639
+the widget.
  640
+
636 641
 ``CheckboxSelectMultiple``
637 642
 ~~~~~~~~~~~~~~~~~~~~~~~~~~
638 643
 
8  tests/forms_tests/tests/test_forms.py
@@ -407,7 +407,7 @@ class FrameworkForm(Form):
407 407
         # gets a distinct ID, formed by appending an underscore plus the button's
408 408
         # zero-based index.
409 409
         f = FrameworkForm(auto_id='id_%s')
410  
-        self.assertHTMLEqual(str(f['language']), """<ul>
  410
+        self.assertHTMLEqual(str(f['language']), """<ul id="id_language">
411 411
 <li><label for="id_language_0"><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
412 412
 <li><label for="id_language_1"><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
413 413
 </ul>""")
@@ -416,17 +416,17 @@ class FrameworkForm(Form):
416 416
         # either as_table() or as_ul(), the label for the RadioSelect will point to the
417 417
         # ID of the *first* radio button.
418 418
         self.assertHTMLEqual(f.as_table(), """<tr><th><label for="id_name">Name:</label></th><td><input type="text" name="name" id="id_name" /></td></tr>
419  
-<tr><th><label for="id_language_0">Language:</label></th><td><ul>
  419
+<tr><th><label for="id_language_0">Language:</label></th><td><ul id="id_language">
420 420
 <li><label for="id_language_0"><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
421 421
 <li><label for="id_language_1"><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
422 422
 </ul></td></tr>""")
423 423
         self.assertHTMLEqual(f.as_ul(), """<li><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></li>
424  
-<li><label for="id_language_0">Language:</label> <ul>
  424
+<li><label for="id_language_0">Language:</label> <ul id="id_language">
425 425
 <li><label for="id_language_0"><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
426 426
 <li><label for="id_language_1"><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
427 427
 </ul></li>""")
428 428
         self.assertHTMLEqual(f.as_p(), """<p><label for="id_name">Name:</label> <input type="text" name="name" id="id_name" /></p>
429  
-<p><label for="id_language_0">Language:</label> <ul>
  429
+<p><label for="id_language_0">Language:</label> <ul id="id_language">
430 430
 <li><label for="id_language_0"><input type="radio" id="id_language_0" value="P" name="language" /> Python</label></li>
431 431
 <li><label for="id_language_1"><input type="radio" id="id_language_1" value="J" name="language" /> Java</label></li>
432 432
 </ul></p>""")
4  tests/forms_tests/tests/test_regressions.py
@@ -54,7 +54,7 @@ class SomeForm(Form):
54 54
             somechoice = ChoiceField(choices=GENDERS, widget=RadioSelect(), label='\xc5\xf8\xdf')
55 55
 
56 56
         f = SomeForm()
57  
-        self.assertHTMLEqual(f.as_p(), '<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>')
  57
+        self.assertHTMLEqual(f.as_p(), '<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>')
58 58
 
59 59
         # Testing choice validation with UTF-8 bytestrings as input (these are the
60 60
         # Russian abbreviations "мес." and "шт.".
@@ -70,7 +70,7 @@ class SomeForm(Form):
70 70
         # Translated error messages used to be buggy.
71 71
         with override('ru'):
72 72
             f = SomeForm({})
73  
-            self.assertHTMLEqual(f.as_p(), '<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>')
  73
+            self.assertHTMLEqual(f.as_p(), '<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>')
74 74
 
75 75
         # Deep copying translated text shouldn't raise an error)
76 76
         from django.utils.translation import gettext_lazy
4  tests/forms_tests/tests/test_widgets.py
@@ -684,7 +684,7 @@ class CustomRadioSelect(RadioSelect):
684 684
 
685 685
         # Attributes provided at instantiation are passed to the constituent inputs
686 686
         w = RadioSelect(attrs={'id':'foo'})
687  
-        self.assertHTMLEqual(w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))), """<ul>
  687
+        self.assertHTMLEqual(w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))), """<ul id="foo">
688 688
 <li><label for="foo_0"><input checked="checked" type="radio" id="foo_0" value="J" name="beatle" /> John</label></li>
689 689
 <li><label for="foo_1"><input type="radio" id="foo_1" value="P" name="beatle" /> Paul</label></li>
690 690
 <li><label for="foo_2"><input type="radio" id="foo_2" value="G" name="beatle" /> George</label></li>
@@ -693,7 +693,7 @@ class CustomRadioSelect(RadioSelect):
693 693
 
694 694
         # Attributes provided at render-time are passed to the constituent inputs
695 695
         w = RadioSelect()
696  
-        self.assertHTMLEqual(w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')), attrs={'id':'bar'}), """<ul>
  696
+        self.assertHTMLEqual(w.render('beatle', 'J', choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')), attrs={'id':'bar'}), """<ul id="bar">
697 697
 <li><label for="bar_0"><input checked="checked" type="radio" id="bar_0" value="J" name="beatle" /> John</label></li>
698 698
 <li><label for="bar_1"><input type="radio" id="bar_1" value="P" name="beatle" /> Paul</label></li>
699 699
 <li><label for="bar_2"><input type="radio" id="bar_2" value="G" name="beatle" /> George</label></li>

0 notes on commit c4186c2

Please sign in to comment.
Something went wrong with that request. Please try again.