[1.11.x] Fixed #27920 -- Restored empty RadioSelect choice producing value=""

Regression in b52c73008a.
Thanks Tim Graham for the review.
Backport of 540ae68a5c from master.
This commit is contained in:
Claude Paroz 2017-03-09 15:42:47 +01:00
parent c654ead898
commit d8e90040e9
6 changed files with 15 additions and 10 deletions

View File

@ -350,13 +350,9 @@ class AdminURLFieldWidget(forms.URLInput):
context = super(AdminURLFieldWidget, self).get_context(name, value, attrs)
context['current_label'] = _('Currently:')
context['change_label'] = _('Change:')
context['widget']['href'] = smart_urlquote(context['widget']['value'])
context['widget']['href'] = smart_urlquote(context['widget']['value']) if value else ''
return context
def format_value(self, value):
value = super(AdminURLFieldWidget, self).format_value(value)
return force_text(value)
class AdminIntegerFieldWidget(forms.NumberInput):
class_name = 'vIntegerField'

View File

@ -1 +1 @@
<input type="{{ widget.type }}" name="{{ widget.name }}"{% if widget.value != None and widget.value != "" %} value="{{ widget.value }}"{% endif %}{% include "django/forms/widgets/attrs.html" %} />
<input type="{{ widget.type }}" name="{{ widget.name }}"{% if widget.value != None %} value="{{ widget.value }}"{% endif %}{% include "django/forms/widgets/attrs.html" %} />

View File

@ -1 +1 @@
<input type="{{ widget.type }}" name="{{ widget.name }}"{% if widget.value != None and widget.value != "" %} value="{{ widget.value }}"{% endif %}{% include "django/forms/widgets/attrs.html" %} />
<input type="{{ widget.type }}" name="{{ widget.name }}"{% if widget.value != None %} value="{{ widget.value }}"{% endif %}{% include "django/forms/widgets/attrs.html" %} />

View File

@ -195,8 +195,8 @@ class Widget(six.with_metaclass(RenameWidgetMethods)):
"""
Return a value as it should appear when rendered in a template.
"""
if value is None:
value = ''
if value == '' or value is None:
return None
if self.is_localized:
return formats.localize_input(value)
return force_text(value)

View File

@ -7,8 +7,10 @@ class RadioSelectTest(WidgetTest):
widget = RadioSelect
def test_render(self):
self.check_html(self.widget(choices=self.beatles), 'beatle', 'J', html=(
choices = (('', '------'),) + self.beatles
self.check_html(self.widget(choices=choices), 'beatle', 'J', html=(
"""<ul>
<li><label><input type="radio" name="beatle" value="" /> ------</label></li>
<li><label><input checked type="radio" name="beatle" value="J" /> John</label></li>
<li><label><input type="radio" name="beatle" value="P" /> Paul</label></li>
<li><label><input type="radio" name="beatle" value="G" /> George</label></li>

View File

@ -8,6 +8,13 @@ from .base import WidgetTest
class WidgetTests(WidgetTest):
def test_format_value(self):
widget = Widget()
self.assertIsNone(widget.format_value(None))
self.assertIsNone(widget.format_value(''))
self.assertEqual(widget.format_value('español'), 'español')
self.assertEqual(widget.format_value(42.5), '42.5')
def test_value_omitted_from_data(self):
widget = Widget()
self.assertIs(widget.value_omitted_from_data({}, {}, 'field'), True)