diff --git a/django/forms/fields.py b/django/forms/fields.py
index ac68b9f..2ed5c4c 100644
a
|
b
|
from django.forms.widgets import (
|
26 | 26 | SplitDateTimeWidget, SplitHiddenDateTimeWidget, FILE_INPUT_CONTRADICTION |
27 | 27 | ) |
28 | 28 | from django.utils import formats |
29 | | from django.utils.encoding import smart_text, force_str, force_text |
| 29 | from django.utils.encoding import smart_text, force_str, force_text, valid_text |
30 | 30 | from django.utils.ipv6 import clean_ipv6_address |
31 | 31 | from django.utils import six |
32 | 32 | from django.utils.translation import ugettext_lazy as _, ungettext_lazy |
… |
… |
class Field(object):
|
82 | 82 | # hidden widget with initial value after widget. |
83 | 83 | # validators -- List of addtional validators to use |
84 | 84 | # localize -- Boolean that specifies if the field should be localized. |
| 85 | if label is not None: |
| 86 | label = valid_text(label) |
85 | 87 | self.required, self.label, self.initial = required, label, initial |
86 | 88 | self.show_hidden_initial = show_hidden_initial |
87 | | self.help_text = help_text |
| 89 | self.help_text = valid_text(help_text) |
88 | 90 | widget = widget or self.widget |
89 | 91 | if isinstance(widget, type): |
90 | 92 | widget = widget() |
diff --git a/django/utils/encoding.py b/django/utils/encoding.py
index adab0d0..400b7b9 100644
a
|
b
|
def force_bytes(s, encoding='utf-8', strings_only=False, errors='strict'):
|
170 | 170 | else: |
171 | 171 | return s.encode(encoding, errors) |
172 | 172 | |
| 173 | def valid_text(value): |
| 174 | """ |
| 175 | Check that value is either ascii bytestring or unicode non-ascii. |
| 176 | """ |
| 177 | if isinstance(value, bytes): |
| 178 | try: |
| 179 | six.text_type(value, 'ascii') |
| 180 | except UnicodeDecodeError: |
| 181 | raise AssertionError("Strings containing non-ASCII chars should be unicode") |
| 182 | return value |
| 183 | |
| 184 | |
173 | 185 | if six.PY3: |
174 | 186 | smart_str = smart_text |
175 | 187 | force_str = force_text |
diff --git a/docs/releases/1.6.txt b/docs/releases/1.6.txt
index 825c5dc..c4bce9c 100644
a
|
b
|
Miscellaneous
|
582 | 582 | changes in 1.6 particularly affect :class:`~django.forms.DecimalField` and |
583 | 583 | :class:`~django.forms.ModelMultipleChoiceField`. |
584 | 584 | |
| 585 | * Non-ASCII bytestrings are not accepted any longer for the ``label`` and |
| 586 | ``help_text`` arguments to form fields ``__init__``. |
| 587 | |
585 | 588 | * There have been changes in the way timeouts are handled in cache backends. |
586 | 589 | Explicitly passing in ``timeout=None`` no longer results in using the |
587 | 590 | default timeout. It will now set a non-expiring timeout. Passing 0 into the |
diff --git a/tests/forms_tests/tests/test_forms.py b/tests/forms_tests/tests/test_forms.py
index 1a3fb44..911ea23 100644
a
|
b
|
class FormsTestCase(TestCase):
|
979 | 979 | <p><label for="id_q4">Answer this question!</label> <input type="text" name="q4" id="id_q4" /></p> |
980 | 980 | <p><label for="id_q5">The last question. Period.</label> <input type="text" name="q5" id="id_q5" /></p>""") |
981 | 981 | |
| 982 | # Label should not be a non-ascii bytestring |
| 983 | with self.assertRaises(AssertionError): |
| 984 | class UserRegistration(Form): |
| 985 | username = CharField(max_length=10) |
| 986 | password = CharField(widget=PasswordInput, label=b'Contrase\xc3\xb1a') |
| 987 | |
982 | 988 | # If a label is set to the empty string for a field, that field won't get a label. |
983 | 989 | class UserRegistration(Form): |
984 | 990 | username = CharField(max_length=10, label='') |
… |
… |
class FormsTestCase(TestCase):
|
1264 | 1270 | self.assertHTMLEqual(p.as_ul(), """<li>Username: <input type="text" name="username" maxlength="10" /> <span class="helptext">e.g., user@example.com</span></li> |
1265 | 1271 | <li>Password: <input type="password" name="password" /><input type="hidden" name="next" value="/" /></li>""") |
1266 | 1272 | |
| 1273 | # help_text should not be a non-ascii bytestring |
| 1274 | with self.assertRaises(AssertionError): |
| 1275 | class UserRegistration(Form): |
| 1276 | username = CharField(max_length=10, help_text=b'W\xc3\xa4hlen Sie bitte') |
| 1277 | |
1267 | 1278 | def test_subclassing_forms(self): |
1268 | 1279 | # You can subclass a Form to add fields. The resulting form subclass will have |
1269 | 1280 | # all of the fields of the parent Form, plus whichever fields you define in the |