From a3b1319d5863600981e71fbaa452d7104715a9e7 Mon Sep 17 00:00:00 2001 From: Tim Graham Date: Thu, 15 Jun 2017 11:05:21 -0400 Subject: [PATCH] [1.11.x] Fixed #28176 -- Restored the uncasted option value in ChoiceWidget template context. Backport of 221e6e18177516ac4ac95e40c344b93d14dd607b from master --- django/forms/templates/django/forms/widgets/input.html | 2 +- .../templates/django/forms/widgets/select_option.html | 2 +- django/forms/widgets.py | 2 +- docs/releases/1.11.3.txt | 7 +++++++ tests/forms_tests/widget_tests/test_select.py | 6 ++++++ 5 files changed, 16 insertions(+), 3 deletions(-) diff --git a/django/forms/templates/django/forms/widgets/input.html b/django/forms/templates/django/forms/widgets/input.html index abbdf6bd26..5feef43c55 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/templates/django/forms/widgets/select_option.html b/django/forms/templates/django/forms/widgets/select_option.html index c6355f69dd..8d31961dd3 100644 --- a/django/forms/templates/django/forms/widgets/select_option.html +++ b/django/forms/templates/django/forms/widgets/select_option.html @@ -1 +1 @@ - + diff --git a/django/forms/widgets.py b/django/forms/widgets.py index e84db8d7c0..c2bc534e5a 100644 --- a/django/forms/widgets.py +++ b/django/forms/widgets.py @@ -611,7 +611,7 @@ class ChoiceWidget(Widget): option_attrs['id'] = self.id_for_label(option_attrs['id'], index) return { 'name': name, - 'value': force_text(value), + 'value': value, 'label': label, 'selected': selected, 'index': index, diff --git a/docs/releases/1.11.3.txt b/docs/releases/1.11.3.txt index 1e2ddddb66..9aab4fbfb7 100644 --- a/docs/releases/1.11.3.txt +++ b/docs/releases/1.11.3.txt @@ -44,3 +44,10 @@ Bugfixes * Prevented attribute values in the ``django/forms/widgets/attrs.html`` template from being localized so that numeric attributes (e.g. ``max`` and ``min``) of ``NumberInput`` work correctly (:ticket:`28303`). + +* Removed casting of the option value to a string in the template context of + the ``CheckboxSelectMultiple``, ``NullBooleanSelect``, ``RadioSelect``, + ``SelectMultiple``, and ``Select`` widgets (:ticket:`28176`). In Django + 1.11.1, casting was added in Python to avoid localization of numeric values + in Django templates, but this made some use cases more difficult. Casting is + now done in the template using the ``|stringformat:'s'`` filter. diff --git a/tests/forms_tests/widget_tests/test_select.py b/tests/forms_tests/widget_tests/test_select.py index d366d29ff3..13dc5ea669 100644 --- a/tests/forms_tests/widget_tests/test_select.py +++ b/tests/forms_tests/widget_tests/test_select.py @@ -351,6 +351,12 @@ class SelectTest(WidgetTest): ) self.assertEqual(index, 2) + def test_optgroups_integer_choices(self): + """The option 'value' is the same type as what's in `choices`.""" + groups = list(self.widget(choices=[[0, 'choice text']]).optgroups('name', ['vhs'])) + label, options, index = groups[0] + self.assertEqual(options[0]['value'], 0) + def test_deepcopy(self): """ __deepcopy__() should copy all attributes properly (#25085).