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 }}
...