Ticket #4860: ca.2.diff

File ca.2.diff, 15.7 KB (added by sandro, 17 years ago)
  • django/newforms/widgets.py

     
    367367            cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values)
    368368            option_value = force_unicode(option_value)
    369369            rendered_cb = cb.render(name, option_value)
    370             output.append(u'<li><label>%s %s</label></li>' % (rendered_cb,
    371                     conditional_escape(force_unicode(option_label))))
     370            for_id = final_attrs.get('id', option_value)
     371            output.append(u'<li>%s <label for="%s">%s</label></li>' % (rendered_cb, for_id, conditional_escape(force_unicode(option_label))))
    372372        output.append(u'</ul>')
    373373        return mark_safe(u'\n'.join(output))
    374374
  • tests/regressiontests/forms/forms.py

     
    494494>>> f = SongForm(auto_id=False)
    495495>>> print f['composers']
    496496<ul>
    497 <li><label><input type="checkbox" name="composers" value="J" /> John Lennon</label></li>
    498 <li><label><input type="checkbox" name="composers" value="P" /> Paul McCartney</label></li>
     497<li><input type="checkbox" name="composers" value="J" /> <label for="J">John Lennon</label></li>
     498<li><input type="checkbox" name="composers" value="P" /> <label for="P">Paul McCartney</label></li>
    499499</ul>
    500500>>> f = SongForm({'composers': ['J']}, auto_id=False)
    501501>>> print f['composers']
    502502<ul>
    503 <li><label><input checked="checked" type="checkbox" name="composers" value="J" /> John Lennon</label></li>
    504 <li><label><input type="checkbox" name="composers" value="P" /> Paul McCartney</label></li>
     503<li><input checked="checked" type="checkbox" name="composers" value="J" /> <label for="J">John Lennon</label></li>
     504<li><input type="checkbox" name="composers" value="P" /> <label for="P">Paul McCartney</label></li>
    505505</ul>
    506506>>> f = SongForm({'composers': ['J', 'P']}, auto_id=False)
    507507>>> print f['composers']
    508508<ul>
    509 <li><label><input checked="checked" type="checkbox" name="composers" value="J" /> John Lennon</label></li>
    510 <li><label><input checked="checked" type="checkbox" name="composers" value="P" /> Paul McCartney</label></li>
     509<li><input checked="checked" type="checkbox" name="composers" value="J" /> <label for="J">John Lennon</label></li>
     510<li><input checked="checked" type="checkbox" name="composers" value="P" /> <label for="P">Paul McCartney</label></li>
    511511</ul>
    512512
    513513Regarding auto_id, CheckboxSelectMultiple is a special case. Each checkbox
     
    516516>>> f = SongForm(auto_id='%s_id')
    517517>>> print f['composers']
    518518<ul>
    519 <li><label><input type="checkbox" name="composers" value="J" id="composers_id_0" /> John Lennon</label></li>
    520 <li><label><input type="checkbox" name="composers" value="P" id="composers_id_1" /> Paul McCartney</label></li>
     519<li><input type="checkbox" name="composers" value="J" id="composers_id_0" /> <label for="composers_id_0">John Lennon</label></li>
     520<li><input type="checkbox" name="composers" value="P" id="composers_id_1" /> <label for="composers_id_1">Paul McCartney</label></li>
    521521</ul>
    522522
    523523Data for a MultipleChoiceField should be a list. QueryDict and MultiValueDict
  • tests/regressiontests/forms/widgets.py

     
    762762>>> w = CheckboxSelectMultiple()
    763763>>> print w.render('beatles', ['J'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
    764764<ul>
    765 <li><label><input checked="checked" type="checkbox" name="beatles" value="J" /> John</label></li>
    766 <li><label><input type="checkbox" name="beatles" value="P" /> Paul</label></li>
    767 <li><label><input type="checkbox" name="beatles" value="G" /> George</label></li>
    768 <li><label><input type="checkbox" name="beatles" value="R" /> Ringo</label></li>
     765<li><input checked="checked" type="checkbox" name="beatles" value="J" /> <label for="J">John</label></li>
     766<li><input type="checkbox" name="beatles" value="P" /> <label for="P">Paul</label></li>
     767<li><input type="checkbox" name="beatles" value="G" /> <label for="G">George</label></li>
     768<li><input type="checkbox" name="beatles" value="R" /> <label for="R">Ringo</label></li>
    769769</ul>
    770770>>> print w.render('beatles', ['J', 'P'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
    771771<ul>
    772 <li><label><input checked="checked" type="checkbox" name="beatles" value="J" /> John</label></li>
    773 <li><label><input checked="checked" type="checkbox" name="beatles" value="P" /> Paul</label></li>
    774 <li><label><input type="checkbox" name="beatles" value="G" /> George</label></li>
    775 <li><label><input type="checkbox" name="beatles" value="R" /> Ringo</label></li>
     772<li><input checked="checked" type="checkbox" name="beatles" value="J" /> <label for="J">John</label></li>
     773<li><input checked="checked" type="checkbox" name="beatles" value="P" /> <label for="P">Paul</label></li>
     774<li><input type="checkbox" name="beatles" value="G" /> <label for="G">George</label></li>
     775<li><input type="checkbox" name="beatles" value="R" /> <label for="R">Ringo</label></li>
    776776</ul>
    777777>>> print w.render('beatles', ['J', 'P', 'R'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
    778778<ul>
    779 <li><label><input checked="checked" type="checkbox" name="beatles" value="J" /> John</label></li>
    780 <li><label><input checked="checked" type="checkbox" name="beatles" value="P" /> Paul</label></li>
    781 <li><label><input type="checkbox" name="beatles" value="G" /> George</label></li>
    782 <li><label><input checked="checked" type="checkbox" name="beatles" value="R" /> Ringo</label></li>
     779<li><input checked="checked" type="checkbox" name="beatles" value="J" /> <label for="J">John</label></li>
     780<li><input checked="checked" type="checkbox" name="beatles" value="P" /> <label for="P">Paul</label></li>
     781<li><input type="checkbox" name="beatles" value="G" /> <label for="G">George</label></li>
     782<li><input checked="checked" type="checkbox" name="beatles" value="R" /> <label for="R">Ringo</label></li>
    783783</ul>
    784784
    785785If the value is None, none of the options are selected:
    786786>>> print w.render('beatles', None, choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
    787787<ul>
    788 <li><label><input type="checkbox" name="beatles" value="J" /> John</label></li>
    789 <li><label><input type="checkbox" name="beatles" value="P" /> Paul</label></li>
    790 <li><label><input type="checkbox" name="beatles" value="G" /> George</label></li>
    791 <li><label><input type="checkbox" name="beatles" value="R" /> Ringo</label></li>
     788<li><input type="checkbox" name="beatles" value="J" /> <label for="J">John</label></li>
     789<li><input type="checkbox" name="beatles" value="P" /> <label for="P">Paul</label></li>
     790<li><input type="checkbox" name="beatles" value="G" /> <label for="G">George</label></li>
     791<li><input type="checkbox" name="beatles" value="R" /> <label for="R">Ringo</label></li>
    792792</ul>
    793793
    794794If the value corresponds to a label (but not to an option value), none of the options are selected:
    795795>>> print w.render('beatles', ['John'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
    796796<ul>
    797 <li><label><input type="checkbox" name="beatles" value="J" /> John</label></li>
    798 <li><label><input type="checkbox" name="beatles" value="P" /> Paul</label></li>
    799 <li><label><input type="checkbox" name="beatles" value="G" /> George</label></li>
    800 <li><label><input type="checkbox" name="beatles" value="R" /> Ringo</label></li>
     797<li><input type="checkbox" name="beatles" value="J" /> <label for="J">John</label></li>
     798<li><input type="checkbox" name="beatles" value="P" /> <label for="P">Paul</label></li>
     799<li><input type="checkbox" name="beatles" value="G" /> <label for="G">George</label></li>
     800<li><input type="checkbox" name="beatles" value="R" /> <label for="R">Ringo</label></li>
    801801</ul>
    802802
    803803If multiple values are given, but some of them are not valid, the valid ones are selected:
    804804>>> print w.render('beatles', ['J', 'G', 'foo'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo')))
    805805<ul>
    806 <li><label><input checked="checked" type="checkbox" name="beatles" value="J" /> John</label></li>
    807 <li><label><input type="checkbox" name="beatles" value="P" /> Paul</label></li>
    808 <li><label><input checked="checked" type="checkbox" name="beatles" value="G" /> George</label></li>
    809 <li><label><input type="checkbox" name="beatles" value="R" /> Ringo</label></li>
     806<li><input checked="checked" type="checkbox" name="beatles" value="J" /> <label for="J">John</label></li>
     807<li><input type="checkbox" name="beatles" value="P" /> <label for="P">Paul</label></li>
     808<li><input checked="checked" type="checkbox" name="beatles" value="G" /> <label for="G">George</label></li>
     809<li><input type="checkbox" name="beatles" value="R" /> <label for="R">Ringo</label></li>
    810810</ul>
    811811
    812812The value is compared to its str():
    813813>>> print w.render('nums', [2], choices=[('1', '1'), ('2', '2'), ('3', '3')])
    814814<ul>
    815 <li><label><input type="checkbox" name="nums" value="1" /> 1</label></li>
    816 <li><label><input checked="checked" type="checkbox" name="nums" value="2" /> 2</label></li>
    817 <li><label><input type="checkbox" name="nums" value="3" /> 3</label></li>
     815<li><input type="checkbox" name="nums" value="1" /> <label for="1">1</label></li>
     816<li><input checked="checked" type="checkbox" name="nums" value="2" /> <label for="2">2</label></li>
     817<li><input type="checkbox" name="nums" value="3" /> <label for="3">3</label></li>
    818818</ul>
    819819>>> print w.render('nums', ['2'], choices=[(1, 1), (2, 2), (3, 3)])
    820820<ul>
    821 <li><label><input type="checkbox" name="nums" value="1" /> 1</label></li>
    822 <li><label><input checked="checked" type="checkbox" name="nums" value="2" /> 2</label></li>
    823 <li><label><input type="checkbox" name="nums" value="3" /> 3</label></li>
     821<li><input type="checkbox" name="nums" value="1" /> <label for="1">1</label></li>
     822<li><input checked="checked" type="checkbox" name="nums" value="2" /> <label for="2">2</label></li>
     823<li><input type="checkbox" name="nums" value="3" /> <label for="3">3</label></li>
    824824</ul>
    825825>>> print w.render('nums', [2], choices=[(1, 1), (2, 2), (3, 3)])
    826826<ul>
    827 <li><label><input type="checkbox" name="nums" value="1" /> 1</label></li>
    828 <li><label><input checked="checked" type="checkbox" name="nums" value="2" /> 2</label></li>
    829 <li><label><input type="checkbox" name="nums" value="3" /> 3</label></li>
     827<li><input type="checkbox" name="nums" value="1" /> <label for="1">1</label></li>
     828<li><input checked="checked" type="checkbox" name="nums" value="2" /> <label for="2">2</label></li>
     829<li><input type="checkbox" name="nums" value="3" /> <label for="3">3</label></li>
    830830</ul>
    831831
    832832The 'choices' argument can be any iterable:
     
    835835...         yield (i, i)
    836836>>> print w.render('nums', [2], choices=get_choices())
    837837<ul>
    838 <li><label><input type="checkbox" name="nums" value="0" /> 0</label></li>
    839 <li><label><input type="checkbox" name="nums" value="1" /> 1</label></li>
    840 <li><label><input checked="checked" type="checkbox" name="nums" value="2" /> 2</label></li>
    841 <li><label><input type="checkbox" name="nums" value="3" /> 3</label></li>
    842 <li><label><input type="checkbox" name="nums" value="4" /> 4</label></li>
     838<li><input type="checkbox" name="nums" value="0" /> <label for="0">0</label></li>
     839<li><input type="checkbox" name="nums" value="1" /> <label for="1">1</label></li>
     840<li><input checked="checked" type="checkbox" name="nums" value="2" /> <label for="2">2</label></li>
     841<li><input type="checkbox" name="nums" value="3" /> <label for="3">3</label></li>
     842<li><input type="checkbox" name="nums" value="4" /> <label for="4">4</label></li>
    843843</ul>
    844844
    845845You can also pass 'choices' to the constructor:
    846846>>> w = CheckboxSelectMultiple(choices=[(1, 1), (2, 2), (3, 3)])
    847847>>> print w.render('nums', [2])
    848848<ul>
    849 <li><label><input type="checkbox" name="nums" value="1" /> 1</label></li>
    850 <li><label><input checked="checked" type="checkbox" name="nums" value="2" /> 2</label></li>
    851 <li><label><input type="checkbox" name="nums" value="3" /> 3</label></li>
     849<li><input type="checkbox" name="nums" value="1" /> <label for="1">1</label></li>
     850<li><input checked="checked" type="checkbox" name="nums" value="2" /> <label for="2">2</label></li>
     851<li><input type="checkbox" name="nums" value="3" /> <label for="3">3</label></li>
    852852</ul>
    853853
    854854If 'choices' is passed to both the constructor and render(), then they'll both be in the output:
    855855>>> print w.render('nums', [2], choices=[(4, 4), (5, 5)])
    856856<ul>
    857 <li><label><input type="checkbox" name="nums" value="1" /> 1</label></li>
    858 <li><label><input checked="checked" type="checkbox" name="nums" value="2" /> 2</label></li>
    859 <li><label><input type="checkbox" name="nums" value="3" /> 3</label></li>
    860 <li><label><input type="checkbox" name="nums" value="4" /> 4</label></li>
    861 <li><label><input type="checkbox" name="nums" value="5" /> 5</label></li>
     857<li><input type="checkbox" name="nums" value="1" /> <label for="1">1</label></li>
     858<li><input checked="checked" type="checkbox" name="nums" value="2" /> <label for="2">2</label></li>
     859<li><input type="checkbox" name="nums" value="3" /> <label for="3">3</label></li>
     860<li><input type="checkbox" name="nums" value="4" /> <label for="4">4</label></li>
     861<li><input type="checkbox" name="nums" value="5" /> <label for="5">5</label></li>
    862862</ul>
    863863
    864864# Choices are escaped correctly
    865865>>> print w.render('escape', None, choices=(('bad', 'you & me'), ('good', mark_safe('you &gt; me'))))
    866866<ul>
    867 <li><label><input type="checkbox" name="escape" value="1" /> 1</label></li>
    868 <li><label><input type="checkbox" name="escape" value="2" /> 2</label></li>
    869 <li><label><input type="checkbox" name="escape" value="3" /> 3</label></li>
    870 <li><label><input type="checkbox" name="escape" value="bad" /> you &amp; me</label></li>
    871 <li><label><input type="checkbox" name="escape" value="good" /> you &gt; me</label></li>
     867<li><input type="checkbox" name="escape" value="1" /> <label for="1">1</label></li>
     868<li><input type="checkbox" name="escape" value="2" /> <label for="2">2</label></li>
     869<li><input type="checkbox" name="escape" value="3" /> <label for="3">3</label></li>
     870<li><input type="checkbox" name="escape" value="bad" /> <label for="bad">you &amp; me</label></li>
     871<li><input type="checkbox" name="escape" value="good" /> <label for="good">you &gt; me</label></li>
    872872</ul>
    873873
    874874# Unicode choices are correctly rendered as HTML
    875875>>> w.render('nums', ['ŠĐĆŽćžšđ'], choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')])
    876 u'<ul>\n<li><label><input type="checkbox" name="nums" value="1" /> 1</label></li>\n<li><label><input type="checkbox" name="nums" value="2" /> 2</label></li>\n<li><label><input type="checkbox" name="nums" value="3" /> 3</label></li>\n<li><label><input checked="checked" type="checkbox" name="nums" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" /> \u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111</label></li>\n<li><label><input type="checkbox" name="nums" value="\u0107\u017e\u0161\u0111" /> abc\u0107\u017e\u0161\u0111</label></li>\n</ul>'
     876u'<ul>\n<li><input type="checkbox" name="nums" value="1" /> <label for="1">1</label></li>\n<li><input type="checkbox" name="nums" value="2" /> <label for="2">2</label></li>\n<li><input type="checkbox" name="nums" value="3" /> <label for="3">3</label></li>\n<li><input checked="checked" type="checkbox" name="nums" value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" /> <label for="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111">\u0160\u0110abc\u0106\u017d\u0107\u017e\u0161\u0111</label></li>\n<li><input type="checkbox" name="nums" value="\u0107\u017e\u0161\u0111" /> <label for="\u0107\u017e\u0161\u0111">abc\u0107\u017e\u0161\u0111</label></li>\n</ul>'
    877877
    878878# MultiWidget #################################################################
    879879
Back to Top