Ticket #4860: ca.2.diff
File ca.2.diff, 15.7 KB (added by , 17 years ago) |
---|
-
django/newforms/widgets.py
367 367 cb = CheckboxInput(final_attrs, check_test=lambda value: value in str_values) 368 368 option_value = force_unicode(option_value) 369 369 rendered_cb = cb.render(name, option_value) 370 output.append(u'<li><label>%s %s</label></li>' % (rendered_cb,371 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)))) 372 372 output.append(u'</ul>') 373 373 return mark_safe(u'\n'.join(output)) 374 374 -
tests/regressiontests/forms/forms.py
494 494 >>> f = SongForm(auto_id=False) 495 495 >>> print f['composers'] 496 496 <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> 499 499 </ul> 500 500 >>> f = SongForm({'composers': ['J']}, auto_id=False) 501 501 >>> print f['composers'] 502 502 <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> 505 505 </ul> 506 506 >>> f = SongForm({'composers': ['J', 'P']}, auto_id=False) 507 507 >>> print f['composers'] 508 508 <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> 511 511 </ul> 512 512 513 513 Regarding auto_id, CheckboxSelectMultiple is a special case. Each checkbox … … 516 516 >>> f = SongForm(auto_id='%s_id') 517 517 >>> print f['composers'] 518 518 <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> 521 521 </ul> 522 522 523 523 Data for a MultipleChoiceField should be a list. QueryDict and MultiValueDict -
tests/regressiontests/forms/widgets.py
762 762 >>> w = CheckboxSelectMultiple() 763 763 >>> print w.render('beatles', ['J'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) 764 764 <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> 769 769 </ul> 770 770 >>> print w.render('beatles', ['J', 'P'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) 771 771 <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> 776 776 </ul> 777 777 >>> print w.render('beatles', ['J', 'P', 'R'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) 778 778 <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> 783 783 </ul> 784 784 785 785 If the value is None, none of the options are selected: 786 786 >>> print w.render('beatles', None, choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) 787 787 <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> 792 792 </ul> 793 793 794 794 If the value corresponds to a label (but not to an option value), none of the options are selected: 795 795 >>> print w.render('beatles', ['John'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) 796 796 <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> 801 801 </ul> 802 802 803 803 If multiple values are given, but some of them are not valid, the valid ones are selected: 804 804 >>> print w.render('beatles', ['J', 'G', 'foo'], choices=(('J', 'John'), ('P', 'Paul'), ('G', 'George'), ('R', 'Ringo'))) 805 805 <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> 810 810 </ul> 811 811 812 812 The value is compared to its str(): 813 813 >>> print w.render('nums', [2], choices=[('1', '1'), ('2', '2'), ('3', '3')]) 814 814 <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> 818 818 </ul> 819 819 >>> print w.render('nums', ['2'], choices=[(1, 1), (2, 2), (3, 3)]) 820 820 <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> 824 824 </ul> 825 825 >>> print w.render('nums', [2], choices=[(1, 1), (2, 2), (3, 3)]) 826 826 <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> 830 830 </ul> 831 831 832 832 The 'choices' argument can be any iterable: … … 835 835 ... yield (i, i) 836 836 >>> print w.render('nums', [2], choices=get_choices()) 837 837 <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> 843 843 </ul> 844 844 845 845 You can also pass 'choices' to the constructor: 846 846 >>> w = CheckboxSelectMultiple(choices=[(1, 1), (2, 2), (3, 3)]) 847 847 >>> print w.render('nums', [2]) 848 848 <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> 852 852 </ul> 853 853 854 854 If 'choices' is passed to both the constructor and render(), then they'll both be in the output: 855 855 >>> print w.render('nums', [2], choices=[(4, 4), (5, 5)]) 856 856 <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> 862 862 </ul> 863 863 864 864 # Choices are escaped correctly 865 865 >>> print w.render('escape', None, choices=(('bad', 'you & me'), ('good', mark_safe('you > me')))) 866 866 <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 & me</label></li>871 <li>< label><input type="checkbox" name="escape" value="good" />you > 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 & me</label></li> 871 <li><input type="checkbox" name="escape" value="good" /> <label for="good">you > me</label></li> 872 872 </ul> 873 873 874 874 # Unicode choices are correctly rendered as HTML 875 875 >>> 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>'876 u'<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>' 877 877 878 878 # MultiWidget ################################################################# 879 879