summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorClaude Paroz <claude@2xlibre.net>2014-07-05 11:40:08 +0200
committerClaude Paroz <claude@2xlibre.net>2014-07-05 11:42:27 +0200
commit9209049211acbe1a53c3dc409dd5fe26edf21634 (patch)
treebeba89ebad59e6d25108c9c083ce019e1a177d83
parent1f8bb95cc2286a882e0f7a4692f77b285d811d11 (diff)
Ensured bound field renders as unicode safe data
Refs #22950.
-rw-r--r--django/forms/forms.py2
-rw-r--r--tests/forms_tests/tests/test_forms.py19
2 files changed, 19 insertions, 2 deletions
diff --git a/django/forms/forms.py b/django/forms/forms.py
index 347eccab4b..e448c69807 100644
--- a/django/forms/forms.py
+++ b/django/forms/forms.py
@@ -563,7 +563,7 @@ class BoundField(object):
name = self.html_name
else:
name = self.html_initial_name
- return widget.render(name, self.value(), attrs=attrs)
+ return force_text(widget.render(name, self.value(), attrs=attrs))
def as_text(self, attrs=None, **kwargs):
"""
diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py
index 23de5024fb..0d8ae663a6 100644
--- a/tests/forms_tests/tests/test_forms.py
+++ b/tests/forms_tests/tests/test_forms.py
@@ -22,7 +22,9 @@ from django.template import Template, Context
from django.test import TestCase
from django.test.utils import str_prefix
from django.utils.datastructures import MultiValueDict, MergeDict
-from django.utils.safestring import mark_safe
+from django.utils.encoding import force_text
+from django.utils.html import format_html
+from django.utils.safestring import mark_safe, SafeData
from django.utils import six
@@ -1375,6 +1377,21 @@ class FormsTestCase(TestCase):
self.assertEqual(bound['password'].value(), 'foo')
self.assertEqual(unbound['password'].value(), None)
+ def test_boundfield_rendering(self):
+ """
+ Python 2 issue: Test that rendering a BoundField with bytestring content
+ doesn't lose it's safe string status (#22950).
+ """
+ class CustomWidget(TextInput):
+ def render(self, name, value, attrs=None):
+ return format_html(str('<input{0} />'), ' id=custom')
+
+ class SampleForm(Form):
+ name = CharField(widget=CustomWidget)
+
+ f = SampleForm(data={'name': 'bar'})
+ self.assertIsInstance(force_text(f['name']), SafeData)
+
def test_initial_datetime_values(self):
now = datetime.datetime.now()
# Nix microseconds (since they should be ignored). #22502