#3344 closed (fixed)
newforms UnicodeEncodeError in EmailField on non-successful validation
Reported by: | bartekr | Owned by: | Adrian Holovaty |
---|---|---|---|
Component: | Forms | Version: | dev |
Severity: | Keywords: | UnicodeEncodeError EmailField gettext Polish Norwegian unicode 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 (last modified by )
newforms EmailField causes UnicodeEncodeError exception in case when value isn't correct.
Exception Type: UnicodeEncodeError Exception Value: 'ascii' codec can't encode character u'\u017a' in position 33: ordinal not in range(128) Exception Location: /System/Library/Frameworks/Python.framework/Versions/2.3/lib/python2.3/site-packages/django/newforms/forms.py in _html_output, line 103
I spent some time for debugging and I found that what probably causes the error is:
RegexField.__init__(self, email_re, max_length, min_length, gettext(u'Enter a valid e-mail address.'), required, widget, label, initial)
newforms/fields.py, line 267 (rev. 4386)
When for debug purposes I changed gettext(u'Enter a valid e-mail address.') to anything else (ex. gettext(u'aaa')), the problem didn't occur and the 'aaa' validation error message was displayed like it should. This is probably a problem with translation file or so, but I'm just a Python/django/gettext newbie and I'm just starting my adventure with all of them, so I'm unable to debug it more.
Attachments (3)
Change History (16)
comment:1 by , 18 years ago
comment:2 by , 18 years ago
Keywords: | gettext Polish added |
---|
comment:3 by , 18 years ago
Description: | modified (diff) |
---|---|
Triage Stage: | Unreviewed → Accepted |
Fixed formatting in description.
comment:4 by , 18 years ago
see #3395, it contains a patch... its due to ValidationErrors containing unicode error messages that cannot be handled by some template tags, or other functions in this case...
comment:5 by , 18 years ago
Keywords: | unicode added |
---|
comment:6 by , 18 years ago
Keywords: | Norwegian added |
---|
Happens with norwegian translation too, any way to fix this, should translations not be in utf-8?
django rev 4490
traceback
Traceback (most recent call last): File "/var/www/vhosts/amc-info.com/httpdocs/magic-removal/django/template/__init__.py" in render_node 718. result = node.render(context) File "/var/www/vhosts/amc-info.com/httpdocs/magic-removal/django/template/__init__.py" in render 768. output = self.filter_expression.resolve(context) File "/var/www/vhosts/amc-info.com/httpdocs/magic-removal/django/template/__init__.py" in resolve 561. obj = resolve_variable(self.var, context) File "/var/www/vhosts/amc-info.com/httpdocs/magic-removal/django/template/__init__.py" in resolve_variable 655. current = current() File "/var/www/vhosts/amc-info.com/httpdocs/magic-removal/django/newforms/forms.py" in as_ul 135. return self._html_output(u'<li>%(errors)s%(label)s %(field)s%(help_text)s</li>', u'<li>%s</li>', '</li>', u' %s', False) File "/var/www/vhosts/amc-info.com/httpdocs/magic-removal/django/newforms/forms.py" in _html_output 116. output.append(normal_row % {'errors': bf_errors, 'label': label, 'field': unicode(bf), 'help_text': help_text}) UnicodeEncodeError at /spoersmaal/ 'ascii' codec can't encode character u'\xe5' in position 43: ordinal not in range(128)
local vars
bf <django.newforms.forms.BoundField object at 0xb60ec86c> bf_errors [u'Dette feltet er p\xe5krevd.'] error_row u'<li>%s</li>' errors_on_separate_row False field <django.newforms.fields.CharField object at 0xb60e8ecc> help_text u'' help_text_html u' %s' hidden_fields [] label u'Overskrift:' name 'overskrift' normal_row u'<li>%(errors)s%(label)s %(field)s%(help_text)s</li>' output [] row_ender '</li>' self <django.newforms.models.SpoersmaalForm object at 0xb620c48c> top_errors []
by , 18 years ago
Attachment: | temporary-fix-until-full-unicode.patch added |
---|
temporary solution for utf-8 gettext translations with newforms, encodes as xmlcharrefs
comment:7 by , 18 years ago
I'm not sure whether StrAndUnicode is the right level to fix the bug, since it's used in many places. Are you sure this doesn't somehow put xmlcharrefs in your database?
comment:8 by , 18 years ago
Quite sure, this is only done for validationerrors. Can seem like StrAndUnicode from the patch, but it's not. :)
by , 18 years ago
Attachment: | decode-before-unicode.patch added |
---|
utf-8 decode before unicode , attached patch to wrong ticket
comment:9 by , 18 years ago
Has patch: | set |
---|
comment:10 by , 18 years ago
The attached patch produces incorrect output for me (I get the XML character entities in the validation message, instead of the swedish characters). I will attach a new patch that fixes the issue without doing going to/from ascii encoding for no reason, and it works :P
by , 18 years ago
Attachment: | 4700-UnicodeEncodeError-newforms.diff added |
---|
smaller fix that does not needlessly convert to/from ascii
comment:11 by , 18 years ago
Keywords: | unicode-branch added |
---|
This has been fixed as a result of the changes in the unicode branch, at least as best I can tell from the description, since nobody has posted a test case. I can render forms with invalid Email fields and missing content in languages using non-ASCII characters in the translated errors without problems.
I'll close this ticket once the unicode branch is merged back to trunk.
comment:12 by , 17 years ago
Resolution: | → fixed |
---|---|
Status: | new → closed |
Just found that it happens only if settings.LANGUAGE_CODE == 'pl'
However, when settings.LANGUAGE_CODE == 'en' and after translation.activate( 'pl' ) - it DOES NOT happen (and I could except it happens, as most of values are translated to Polish).