mirror of
https://github.com/django/django.git
synced 2024-12-23 01:25:58 +00:00
Fixed #25731 -- Removed unused choices kwarg for Select.render()
This commit is contained in:
parent
468d8211df
commit
926e90132d
@ -37,7 +37,7 @@ class FilteredSelectMultiple(forms.SelectMultiple):
|
||||
self.is_stacked = is_stacked
|
||||
super(FilteredSelectMultiple, self).__init__(attrs, choices)
|
||||
|
||||
def render(self, name, value, attrs=None, choices=()):
|
||||
def render(self, name, value, attrs=None):
|
||||
if attrs is None:
|
||||
attrs = {}
|
||||
attrs['class'] = 'selectfilter'
|
||||
@ -46,7 +46,7 @@ class FilteredSelectMultiple(forms.SelectMultiple):
|
||||
|
||||
attrs['data-field-name'] = self.verbose_name
|
||||
attrs['data-is-stacked'] = int(self.is_stacked)
|
||||
output = super(FilteredSelectMultiple, self).render(name, value, attrs, choices)
|
||||
output = super(FilteredSelectMultiple, self).render(name, value, attrs)
|
||||
return mark_safe(output)
|
||||
|
||||
|
||||
|
@ -514,12 +514,12 @@ class Select(Widget):
|
||||
memo[id(self)] = obj
|
||||
return obj
|
||||
|
||||
def render(self, name, value, attrs=None, choices=()):
|
||||
def render(self, name, value, attrs=None):
|
||||
if value is None:
|
||||
value = ''
|
||||
final_attrs = self.build_attrs(attrs, name=name)
|
||||
output = [format_html('<select{}>', flatatt(final_attrs))]
|
||||
options = self.render_options(choices, [value])
|
||||
options = self.render_options([value])
|
||||
if options:
|
||||
output.append(options)
|
||||
output.append('</select>')
|
||||
@ -541,11 +541,11 @@ class Select(Widget):
|
||||
selected_html,
|
||||
force_text(option_label))
|
||||
|
||||
def render_options(self, choices, selected_choices):
|
||||
def render_options(self, selected_choices):
|
||||
# Normalize to strings.
|
||||
selected_choices = set(force_text(v) for v in selected_choices)
|
||||
output = []
|
||||
for option_value, option_label in chain(self.choices, choices):
|
||||
for option_value, option_label in self.choices:
|
||||
if isinstance(option_label, (list, tuple)):
|
||||
output.append(format_html('<optgroup label="{}">', force_text(option_value)))
|
||||
for option in option_label:
|
||||
@ -566,12 +566,12 @@ class NullBooleanSelect(Select):
|
||||
('3', ugettext_lazy('No')))
|
||||
super(NullBooleanSelect, self).__init__(attrs, choices)
|
||||
|
||||
def render(self, name, value, attrs=None, choices=()):
|
||||
def render(self, name, value, attrs=None):
|
||||
try:
|
||||
value = {True: '2', False: '3', '2': '2', '3': '3'}[value]
|
||||
except KeyError:
|
||||
value = '1'
|
||||
return super(NullBooleanSelect, self).render(name, value, attrs, choices)
|
||||
return super(NullBooleanSelect, self).render(name, value, attrs)
|
||||
|
||||
def value_from_datadict(self, data, files, name):
|
||||
value = data.get(name)
|
||||
@ -586,12 +586,12 @@ class NullBooleanSelect(Select):
|
||||
class SelectMultiple(Select):
|
||||
allow_multiple_selected = True
|
||||
|
||||
def render(self, name, value, attrs=None, choices=()):
|
||||
def render(self, name, value, attrs=None):
|
||||
if value is None:
|
||||
value = []
|
||||
final_attrs = self.build_attrs(attrs, name=name)
|
||||
output = [format_html('<select multiple="multiple"{}>', flatatt(final_attrs))]
|
||||
options = self.render_options(choices, value)
|
||||
options = self.render_options(value)
|
||||
if options:
|
||||
output.append(options)
|
||||
output.append('</select>')
|
||||
@ -625,7 +625,7 @@ class ChoiceInput(SubWidget):
|
||||
def __str__(self):
|
||||
return self.render()
|
||||
|
||||
def render(self, name=None, value=None, attrs=None, choices=()):
|
||||
def render(self, name=None, value=None, attrs=None):
|
||||
if self.id_for_label:
|
||||
label_for = format_html(' for="{}"', self.id_for_label)
|
||||
else:
|
||||
|
@ -483,6 +483,11 @@ Miscellaneous
|
||||
* The default error views now raise ``TemplateDoesNotExist`` if a nonexistent
|
||||
``template_name`` is specified.
|
||||
|
||||
* The unused ``choices`` keyword argument of the ``Select`` and
|
||||
``SelectMultiple`` widgets' ``render()`` method is removed. The ``choices``
|
||||
argument of the ``render_options()`` method is also removed, making
|
||||
``selected_choices`` the first argument.
|
||||
|
||||
.. _deprecated-features-1.10:
|
||||
|
||||
Features deprecated in 1.10
|
||||
|
@ -10,14 +10,14 @@ from .base import WidgetTest
|
||||
|
||||
|
||||
class SelectTest(WidgetTest):
|
||||
widget = Select()
|
||||
widget = Select
|
||||
nested_widget = Select(choices=(
|
||||
('outer1', 'Outer 1'),
|
||||
('Group "1"', (('inner1', 'Inner 1'), ('inner2', 'Inner 2'))),
|
||||
))
|
||||
|
||||
def test_render(self):
|
||||
self.check_html(self.widget, 'beatle', 'J', choices=self.beatles, html=(
|
||||
self.check_html(self.widget(choices=self.beatles), 'beatle', 'J', html=(
|
||||
"""<select name="beatle">
|
||||
<option value="J" selected="selected">John</option>
|
||||
<option value="P">Paul</option>
|
||||
@ -30,7 +30,7 @@ class SelectTest(WidgetTest):
|
||||
"""
|
||||
If the value is None, none of the options are selected.
|
||||
"""
|
||||
self.check_html(self.widget, 'beatle', None, choices=self.beatles, html=(
|
||||
self.check_html(self.widget(choices=self.beatles), 'beatle', None, html=(
|
||||
"""<select name="beatle">
|
||||
<option value="J">John</option>
|
||||
<option value="P">Paul</option>
|
||||
@ -44,7 +44,7 @@ class SelectTest(WidgetTest):
|
||||
If the value corresponds to a label (but not to an option value), none
|
||||
of the options are selected.
|
||||
"""
|
||||
self.check_html(self.widget, 'beatle', 'John', choices=self.beatles, html=(
|
||||
self.check_html(self.widget(choices=self.beatles), 'beatle', 'John', html=(
|
||||
"""<select name="beatle">
|
||||
<option value="J">John</option>
|
||||
<option value="P">Paul</option>
|
||||
@ -59,7 +59,7 @@ class SelectTest(WidgetTest):
|
||||
"""
|
||||
choices = [('0', '0'), ('1', '1'), ('2', '2'), ('3', '3'), ('0', 'extra')]
|
||||
|
||||
self.check_html(self.widget, 'choices', '0', choices=choices, html=(
|
||||
self.check_html(self.widget(choices=choices), 'choices', '0', html=(
|
||||
"""<select name="choices">
|
||||
<option value="0" selected="selected">0</option>
|
||||
<option value="1">1</option>
|
||||
@ -90,8 +90,8 @@ class SelectTest(WidgetTest):
|
||||
The value is compared to its str().
|
||||
"""
|
||||
self.check_html(
|
||||
self.widget, 'num', 2,
|
||||
choices=[('1', '1'), ('2', '2'), ('3', '3')],
|
||||
self.widget(choices=[('1', '1'), ('2', '2'), ('3', '3')]),
|
||||
'num', 2,
|
||||
html=(
|
||||
"""<select name="num">
|
||||
<option value="1">1</option>
|
||||
@ -101,8 +101,8 @@ class SelectTest(WidgetTest):
|
||||
),
|
||||
)
|
||||
self.check_html(
|
||||
self.widget, 'num', '2',
|
||||
choices=[(1, 1), (2, 2), (3, 3)],
|
||||
self.widget(choices=[(1, 1), (2, 2), (3, 3)]),
|
||||
'num', '2',
|
||||
html=(
|
||||
"""<select name="num">
|
||||
<option value="1">1</option>
|
||||
@ -112,8 +112,8 @@ class SelectTest(WidgetTest):
|
||||
),
|
||||
)
|
||||
self.check_html(
|
||||
self.widget, 'num', 2,
|
||||
choices=[(1, 1), (2, 2), (3, 3)],
|
||||
self.widget(choices=[(1, 1), (2, 2), (3, 3)]),
|
||||
'num', 2,
|
||||
html=(
|
||||
"""<select name="num">
|
||||
<option value="1">1</option>
|
||||
@ -162,25 +162,9 @@ class SelectTest(WidgetTest):
|
||||
</select>"""
|
||||
))
|
||||
|
||||
def test_choices_constuctor_and_render(self):
|
||||
"""
|
||||
If 'choices' is passed to both the constructor and render(), then
|
||||
they'll both be in the output.
|
||||
"""
|
||||
widget = Select(choices=[(1, 1), (2, 2), (3, 3)])
|
||||
self.check_html(widget, 'num', 2, choices=[(4, 4), (5, 5)], html=(
|
||||
"""<select name="num">
|
||||
<option value="1">1</option>
|
||||
<option value="2" selected="selected">2</option>
|
||||
<option value="3">3</option>
|
||||
<option value="4">4</option>
|
||||
<option value="5">5</option>
|
||||
</select>"""
|
||||
))
|
||||
|
||||
def test_choices_escaping(self):
|
||||
choices = (('bad', 'you & me'), ('good', mark_safe('you > me')))
|
||||
self.check_html(self.widget, 'escape', None, choices=choices, html=(
|
||||
self.check_html(self.widget(choices=choices), 'escape', None, html=(
|
||||
"""<select name="escape">
|
||||
<option value="bad">you & me</option>
|
||||
<option value="good">you > me</option>
|
||||
@ -189,8 +173,8 @@ class SelectTest(WidgetTest):
|
||||
|
||||
def test_choices_unicode(self):
|
||||
self.check_html(
|
||||
self.widget, 'email', 'ŠĐĆŽćžšđ',
|
||||
choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')],
|
||||
self.widget(choices=[('ŠĐĆŽćžšđ', 'ŠĐabcĆŽćžšđ'), ('ćžšđ', 'abcćžšđ')]),
|
||||
'email', 'ŠĐĆŽćžšđ',
|
||||
html=(
|
||||
"""<select name="email">
|
||||
<option value="\u0160\u0110\u0106\u017d\u0107\u017e\u0161\u0111" selected="selected">
|
||||
|
@ -4,11 +4,11 @@ from .base import WidgetTest
|
||||
|
||||
|
||||
class SelectMultipleTest(WidgetTest):
|
||||
widget = SelectMultiple()
|
||||
widget = SelectMultiple
|
||||
numeric_choices = (('0', '0'), ('1', '1'), ('2', '2'), ('3', '3'), ('0', 'extra'))
|
||||
|
||||
def test_render_selected(self):
|
||||
self.check_html(self.widget, 'beatles', ['J'], choices=self.beatles, html=(
|
||||
self.check_html(self.widget(choices=self.beatles), 'beatles', ['J'], html=(
|
||||
"""<select multiple="multiple" name="beatles">
|
||||
<option value="J" selected="selected">John</option>
|
||||
<option value="P">Paul</option>
|
||||
@ -18,7 +18,7 @@ class SelectMultipleTest(WidgetTest):
|
||||
))
|
||||
|
||||
def test_render_multiple_selected(self):
|
||||
self.check_html(self.widget, 'beatles', ['J', 'P'], choices=self.beatles, html=(
|
||||
self.check_html(self.widget(choices=self.beatles), 'beatles', ['J', 'P'], html=(
|
||||
"""<select multiple="multiple" name="beatles">
|
||||
<option value="J" selected="selected">John</option>
|
||||
<option value="P" selected="selected">Paul</option>
|
||||
@ -31,7 +31,7 @@ class SelectMultipleTest(WidgetTest):
|
||||
"""
|
||||
If the value is None, none of the options are selected.
|
||||
"""
|
||||
self.check_html(self.widget, 'beatles', None, choices=self.beatles, html=(
|
||||
self.check_html(self.widget(choices=self.beatles), 'beatles', None, html=(
|
||||
"""<select multiple="multiple" name="beatles">
|
||||
<option value="J">John</option>
|
||||
<option value="P">Paul</option>
|
||||
@ -45,7 +45,7 @@ class SelectMultipleTest(WidgetTest):
|
||||
If the value corresponds to a label (but not to an option value), none
|
||||
of the options are selected.
|
||||
"""
|
||||
self.check_html(self.widget, 'beatles', ['John'], choices=self.beatles, html=(
|
||||
self.check_html(self.widget(choices=self.beatles), 'beatles', ['John'], html=(
|
||||
"""<select multiple="multiple" name="beatles">
|
||||
<option value="J">John</option>
|
||||
<option value="P">Paul</option>
|
||||
@ -58,7 +58,7 @@ class SelectMultipleTest(WidgetTest):
|
||||
"""
|
||||
Multiple options with the same value can be selected (#8103).
|
||||
"""
|
||||
self.check_html(self.widget, 'choices', ['0'], choices=self.numeric_choices, html=(
|
||||
self.check_html(self.widget(choices=self.numeric_choices), 'choices', ['0'], html=(
|
||||
"""<select multiple="multiple" name="choices">
|
||||
<option value="0" selected="selected">0</option>
|
||||
<option value="1">1</option>
|
||||
@ -73,7 +73,7 @@ class SelectMultipleTest(WidgetTest):
|
||||
If multiple values are given, but some of them are not valid, the valid
|
||||
ones are selected.
|
||||
"""
|
||||
self.check_html(self.widget, 'beatles', ['J', 'G', 'foo'], choices=self.beatles, html=(
|
||||
self.check_html(self.widget(choices=self.beatles), 'beatles', ['J', 'G', 'foo'], html=(
|
||||
"""<select multiple="multiple" name="beatles">
|
||||
<option value="J" selected="selected">John</option>
|
||||
<option value="P">Paul</option>
|
||||
@ -85,7 +85,7 @@ class SelectMultipleTest(WidgetTest):
|
||||
def test_compare_string(self):
|
||||
choices = [('1', '1'), ('2', '2'), ('3', '3')]
|
||||
|
||||
self.check_html(self.widget, 'nums', [2], choices=choices, html=(
|
||||
self.check_html(self.widget(choices=choices), 'nums', [2], html=(
|
||||
"""<select multiple="multiple" name="nums">
|
||||
<option value="1">1</option>
|
||||
<option value="2" selected="selected">2</option>
|
||||
@ -93,7 +93,7 @@ class SelectMultipleTest(WidgetTest):
|
||||
</select>"""
|
||||
))
|
||||
|
||||
self.check_html(self.widget, 'nums', ['2'], choices=choices, html=(
|
||||
self.check_html(self.widget(choices=choices), 'nums', ['2'], html=(
|
||||
"""<select multiple="multiple" name="nums">
|
||||
<option value="1">1</option>
|
||||
<option value="2" selected="selected">2</option>
|
||||
@ -101,7 +101,7 @@ class SelectMultipleTest(WidgetTest):
|
||||
</select>"""
|
||||
))
|
||||
|
||||
self.check_html(self.widget, 'nums', [2], choices=choices, html=(
|
||||
self.check_html(self.widget(choices=choices), 'nums', [2], html=(
|
||||
"""<select multiple="multiple" name="nums">
|
||||
<option value="1">1</option>
|
||||
<option value="2" selected="selected">2</option>
|
||||
|
Loading…
Reference in New Issue
Block a user