Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #28075 -- Prevented ChoiceWidget from localizing option values. #8385

Merged
merged 1 commit into from Apr 21, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion django/forms/widgets.py
Expand Up @@ -594,7 +594,7 @@ def create_option(self, name, value, label, selected, index, subindex=None, attr
option_attrs['id'] = self.id_for_label(option_attrs['id'], index)
return {
'name': name,
'value': value,
'value': str(value),
'label': label,
'selected': selected,
'index': index,
Expand Down
4 changes: 4 additions & 0 deletions docs/releases/1.11.1.txt
Expand Up @@ -45,3 +45,7 @@ Bugfixes

* Fixed exception reraising in ORM query execution when ``cursor.execute()``
fails and the subsequent ``cursor.close()`` also fails (:ticket:`28091`).

* Fixed a regression where ``CheckboxSelectMultiple``, ``NullBooleanSelect``,
``RadioSelect``, ``SelectMultiple``, and ``Select`` localized option values
(:ticket:`28075`).
31 changes: 31 additions & 0 deletions tests/forms_tests/widget_tests/test_checkboxselectmultiple.py
@@ -1,5 +1,8 @@
import datetime

from django import forms
from django.forms import CheckboxSelectMultiple
from django.test import override_settings

from .base import WidgetTest

Expand Down Expand Up @@ -149,6 +152,34 @@ def test_separate_ids_constructor(self):
"""
self.check_html(widget, 'letters', ['a', 'c'], html=html)

@override_settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True)
def test_doesnt_localize_input_value(self):
choices = [
(1, 'One'),
(1000, 'One thousand'),
(1000000, 'One million'),
]
html = """
<ul>
<li><label><input type="checkbox" name="numbers" value="1" /> One</label></li>
<li><label><input type="checkbox" name="numbers" value="1000" /> One thousand</label></li>
<li><label><input type="checkbox" name="numbers" value="1000000" /> One million</label></li>
</ul>
"""
self.check_html(self.widget(choices=choices), 'numbers', None, html=html)

choices = [
(datetime.time(0, 0), 'midnight'),
(datetime.time(12, 0), 'noon'),
]
html = """
<ul>
<li><label><input type="checkbox" name="times" value="00:00:00" /> midnight</label></li>
<li><label><input type="checkbox" name="times" value="12:00:00" /> noon</label></li>
</ul>
"""
self.check_html(self.widget(choices=choices), 'times', None, html=html)

def test_use_required_attribute(self):
widget = self.widget(choices=self.beatles)
# Always False because browser validation would require all checkboxes
Expand Down
31 changes: 31 additions & 0 deletions tests/forms_tests/widget_tests/test_radioselect.py
@@ -1,4 +1,7 @@
import datetime

from django.forms import RadioSelect
from django.test import override_settings

from .base import WidgetTest

Expand Down Expand Up @@ -99,3 +102,31 @@ def test_class_attrs(self):
</ul>
"""
self.check_html(self.widget(choices=self.beatles), 'beatle', 'J', attrs={'class': 'bar'}, html=html)

@override_settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True)
def test_doesnt_localize_input_value(self):
choices = [
(1, 'One'),
(1000, 'One thousand'),
(1000000, 'One million'),
]
html = """
<ul>
<li><label><input type="radio" name="number" value="1" /> One</label></li>
<li><label><input type="radio" name="number" value="1000" /> One thousand</label></li>
<li><label><input type="radio" name="number" value="1000000" /> One million</label></li>
</ul>
"""
self.check_html(self.widget(choices=choices), 'number', None, html=html)

choices = [
(datetime.time(0, 0), 'midnight'),
(datetime.time(12, 0), 'noon'),
]
html = """
<ul>
<li><label><input type="radio" name="time" value="00:00:00" /> midnight</label></li>
<li><label><input type="radio" name="time" value="12:00:00" /> noon</label></li>
</ul>
"""
self.check_html(self.widget(choices=choices), 'time', None, html=html)
30 changes: 30 additions & 0 deletions tests/forms_tests/widget_tests/test_select.py
@@ -1,6 +1,8 @@
import copy
import datetime

from django.forms import Select
from django.test import override_settings
from django.utils.safestring import mark_safe

from .base import WidgetTest
Expand Down Expand Up @@ -218,6 +220,34 @@ def test_choices_select_inner(self):
</select>"""
))

@override_settings(USE_L10N=True, USE_THOUSAND_SEPARATOR=True)
def test_doesnt_localize_option_value(self):
choices = [
(1, 'One'),
(1000, 'One thousand'),
(1000000, 'One million'),
]
html = """
<select name="number">
<option value="1">One</option>
<option value="1000">One thousand</option>
<option value="1000000">One million</option>
</select>
"""
self.check_html(self.widget(choices=choices), 'number', None, html=html)

choices = [
(datetime.time(0, 0), 'midnight'),
(datetime.time(12, 0), 'noon'),
]
html = """
<select name="time">
<option value="00:00:00">midnight</option>
<option value="12:00:00">noon</option>
</select>
"""
self.check_html(self.widget(choices=choices), 'time', None, html=html)

def test_options(self):
options = list(self.widget(choices=self.beatles).options(
'name', ['J'], attrs={'class': 'super'},
Expand Down