From 540ae68a5cc6339c72375fad9b04dc48f3af4c93 Mon Sep 17 00:00:00 2001 From: Claude Paroz Date: Thu, 9 Mar 2017 15:42:47 +0100 Subject: [PATCH] Fixed #27920 -- Restored empty RadioSelect choice producing value="" Regression in b52c73008a9d67e9ddbb841872dc15cdd3d6ee01. Thanks Tim Graham for the review. --- django/contrib/admin/widgets.py | 6 +----- django/forms/jinja2/django/forms/widgets/input.html | 2 +- django/forms/templates/django/forms/widgets/input.html | 2 +- django/forms/widgets.py | 4 ++-- tests/forms_tests/widget_tests/test_radioselect.py | 4 +++- tests/forms_tests/widget_tests/test_widget.py | 7 +++++++ 6 files changed, 15 insertions(+), 10 deletions(-) diff --git a/django/contrib/admin/widgets.py b/django/contrib/admin/widgets.py index 588bfb58e3..fd57436c46 100644 --- a/django/contrib/admin/widgets.py +++ b/django/contrib/admin/widgets.py @@ -347,13 +347,9 @@ class AdminURLFieldWidget(forms.URLInput): context = super().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().format_value(value) - return force_text(value) - class AdminIntegerFieldWidget(forms.NumberInput): class_name = 'vIntegerField' diff --git a/django/forms/jinja2/django/forms/widgets/input.html b/django/forms/jinja2/django/forms/widgets/input.html index 7e70d1953f..abbdf6bd26 100644 --- a/django/forms/jinja2/django/forms/widgets/input.html +++ b/django/forms/jinja2/django/forms/widgets/input.html @@ -1 +1 @@ - + diff --git a/django/forms/templates/django/forms/widgets/input.html b/django/forms/templates/django/forms/widgets/input.html index 7e70d1953f..abbdf6bd26 100644 --- a/django/forms/templates/django/forms/widgets/input.html +++ b/django/forms/templates/django/forms/widgets/input.html @@ -1 +1 @@ - + diff --git a/django/forms/widgets.py b/django/forms/widgets.py index 7db4c69aad..8b1069d040 100644 --- a/django/forms/widgets.py +++ b/django/forms/widgets.py @@ -180,8 +180,8 @@ class Widget(metaclass=MediaDefiningClass): """ 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) diff --git a/tests/forms_tests/widget_tests/test_radioselect.py b/tests/forms_tests/widget_tests/test_radioselect.py index fea26cece1..58b2fc408e 100644 --- a/tests/forms_tests/widget_tests/test_radioselect.py +++ b/tests/forms_tests/widget_tests/test_radioselect.py @@ -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=( """