1
0
mirror of https://github.com/django/django.git synced 2025-07-05 02:09:13 +00:00

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
This commit is contained in:
Malcolm Tredinnick 2007-05-14 16:06:27 +00:00
parent 3916cfbaff
commit fbfefbf83d
4 changed files with 16 additions and 5 deletions

View File

@ -359,7 +359,7 @@ class ChoiceField(Field):
value = smart_unicode(value) value = smart_unicode(value)
if value == u'': if value == u'':
return value 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: if value not in valid_values:
raise ValidationError(ugettext(u'Select a valid choice. That choice is not one of the available choices.')) raise ValidationError(ugettext(u'Select a valid choice. That choice is not one of the available choices.'))
return value return value

View File

@ -4,7 +4,7 @@ Form classes
from django.utils.datastructures import SortedDict, MultiValueDict from django.utils.datastructures import SortedDict, MultiValueDict
from django.utils.html import escape 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 fields import Field
from widgets import TextInput, Textarea, HiddenInput, MultipleHiddenInput from widgets import TextInput, Textarea, HiddenInput, MultipleHiddenInput
from util import flatatt, ErrorDict, ErrorList, ValidationError from util import flatatt, ErrorDict, ErrorList, ValidationError
@ -311,8 +311,8 @@ class BoundField(StrAndUnicode):
associated Form has specified auto_id. Returns an empty string otherwise. associated Form has specified auto_id. Returns an empty string otherwise.
""" """
auto_id = self.form.auto_id auto_id = self.form.auto_id
if auto_id and '%s' in str(auto_id): if auto_id and '%s' in smart_unicode(auto_id):
return str(auto_id) % self.html_name return smart_unicode(auto_id) % self.html_name
elif auto_id: elif auto_id:
return self.html_name return self.html_name
return '' return ''

View File

@ -4,6 +4,7 @@ and database field objects.
""" """
from django.utils.translation import ugettext from django.utils.translation import ugettext
from django.utils.encoding import smart_unicode
from util import ValidationError from util import ValidationError
from forms import BaseForm, DeclarativeFieldsMetaclass, SortedDictFromList from forms import BaseForm, DeclarativeFieldsMetaclass, SortedDictFromList
from fields import Field, ChoiceField from fields import Field, ChoiceField
@ -120,7 +121,7 @@ class QuerySetIterator(object):
if self.empty_label is not None: if self.empty_label is not None:
yield (u"", self.empty_label) yield (u"", self.empty_label)
for obj in self.queryset: 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. # Clear the QuerySet cache if required.
if not self.cache_choices: if not self.cache_choices:
self.queryset._result_cache = None self.queryset._result_cache = None

View File

@ -36,4 +36,14 @@ Unicode decoding problems...
>>> f = SomeForm() >>> f = SomeForm()
>>> f.as_p() >>> 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>' 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.'
""" """