#4291 closed (fixed)
Unicode bug with gettext variables in newforms/widgets.py
Reported by: | Owned by: | Adrian Holovaty | |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | Keywords: | unicode gettext widget unicode-branch | |
Cc: | Triage Stage: | Accepted | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
newforms/widgets.py
has a bug related to gettext
. I have a select widget in a form template with several options, translated with gettext_lazy
. The rendering of the template fails with an UnicodeDecodeError
exception with a message like
'ascii' codec can't decode byte 0xc3 in position 8: ordinal not in range(128)
Following the callback, trouble arises from calls like this in newforms/widgets.py
:
escape(smart_unicode(string_variable_with_non_english_chars_translated_by_gettext))
The fix is simple: exchange the order of the calls to escape()
and smart_unicode()
:
smart_unicode(escape(string_variable_with_non_english_chars_translated_by_gettext))
These are some code snippets that mimic the application in which I detected the problem:
model.py:
FOO_LIST = (('A', _('A msgid'),) class FooModel(models.Model): a_list = models.StringField(choices=FOO_LIST)
django.po:
msgid "A msgid" msgstr "Nön-ÁSCII chäráctèrs"
view.py:
FooForm = forms.form_for_model(FooModel) f = FooForm() return render_to_response('footemplate.html', {'f': f})
footemplate.html:
... {{ f }} ...
Attachments (1)
Change History (5)
by , 17 years ago
Attachment: | newforms_widget_smart_unicode.patch added |
---|
comment:1 by , 17 years ago
Keywords: | unicode-branch added |
---|---|
Triage Stage: | Unreviewed → Accepted |
This is one of the key problems we are fixing in the unicode branch. Since there isn't a ticket open for this at the moment, I'll leave this open so that if anybody else repeats it, we have something to point them at..
comment:2 by , 17 years ago
With LANGUAGE_CODE="pt-br", using the DateSelectWidget from \newforms\widgets\extras\widgets.py, on revision 5381, I get an UnicodeDecodeError on \newforms\widgets.py line 173.
Exception Type: UnicodeDecodeError
Exception Value: 'ascii' codec can't decode byte 0xc3 in position 3: ordinal not in range(128)
Exception Location: D:\Python\lib\site-packages\django\newforms\widgets.py in render, line 173
- output.append(u'<option value="%s"%s>%s</option>' % (escape(option_value), selected_html, escape(smart_unicode(option_label))))
Changing from "escape(smart_unicode(option_label))" to "smart_unicode(escape(option_label))" solved the problem for me too.
comment:3 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
That works for me!