Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

unicode: Changed a few more places in newforms where str() was being …

…used with

potential non-ASCII arguments. Refs #3406 (and added a test for the latter).


git-svn-id: http://code.djangoproject.com/svn/django/branches/unicode@5236 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit fbfefbf83d1396db1ea4095b2b64e1841689b364 1 parent 3916cfb
@malcolmt malcolmt authored
View
2  django/newforms/fields.py
@@ -359,7 +359,7 @@ def clean(self, value):
value = smart_unicode(value)
if value == u'':
return value
- valid_values = set([str(k) for k, v in self.choices])
+ valid_values = set([smart_unicode(k) for k, v in self.choices])
if value not in valid_values:
raise ValidationError(ugettext(u'Select a valid choice. That choice is not one of the available choices.'))
return value
View
6 django/newforms/forms.py
@@ -4,7 +4,7 @@
from django.utils.datastructures import SortedDict, MultiValueDict
from django.utils.html import escape
-from django.utils.encoding import StrAndUnicode
+from django.utils.encoding import StrAndUnicode, smart_unicode
from fields import Field
from widgets import TextInput, Textarea, HiddenInput, MultipleHiddenInput
from util import flatatt, ErrorDict, ErrorList, ValidationError
@@ -311,8 +311,8 @@ def _auto_id(self):
associated Form has specified auto_id. Returns an empty string otherwise.
"""
auto_id = self.form.auto_id
- if auto_id and '%s' in str(auto_id):
- return str(auto_id) % self.html_name
+ if auto_id and '%s' in smart_unicode(auto_id):
+ return smart_unicode(auto_id) % self.html_name
elif auto_id:
return self.html_name
return ''
View
3  django/newforms/models.py
@@ -4,6 +4,7 @@
"""
from django.utils.translation import ugettext
+from django.utils.encoding import smart_unicode
from util import ValidationError
from forms import BaseForm, DeclarativeFieldsMetaclass, SortedDictFromList
from fields import Field, ChoiceField
@@ -120,7 +121,7 @@ def __iter__(self):
if self.empty_label is not None:
yield (u"", self.empty_label)
for obj in self.queryset:
- yield (obj._get_pk_val(), str(obj))
+ yield (obj._get_pk_val(), smart_unicode(obj))
# Clear the QuerySet cache if required.
if not self.cache_choices:
self.queryset._result_cache = None
View
10 tests/regressiontests/forms/regressions.py
@@ -36,4 +36,14 @@
>>> f = SomeForm()
>>> f.as_p()
u'<p><label for="id_somechoice_0">\xc5\xf8\xdf:</label> <ul>\n<li><label><input type="radio" id="id_somechoice_0" value="\xc5" name="somechoice" /> En tied\xe4</label></li>\n<li><label><input type="radio" id="id_somechoice_1" value="\xf8" name="somechoice" /> Mies</label></li>\n<li><label><input type="radio" id="id_somechoice_2" value="\xdf" name="somechoice" /> Nainen</label></li>\n</ul></p>'
+
+Testing choice validation with UTF-8 bytestrings as input (these are the
+Russian abbreviations "мес." and "шт.".
+
+>>> UNITS = (('\xd0\xbc\xd0\xb5\xd1\x81.', '\xd0\xbc\xd0\xb5\xd1\x81.'), ('\xd1\x88\xd1\x82.', '\xd1\x88\xd1\x82.'))
+>>> f = ChoiceField(choices=UNITS)
+>>> f.clean(u'\u0448\u0442.')
+u'\u0448\u0442.'
+>>> f.clean('\xd1\x88\xd1\x82.')
+u'\u0448\u0442.'
"""
Please sign in to comment.
Something went wrong with that request. Please try again.