user1931780 user1931780 - 5 months ago 18
JSON Question

DjangoUnicodeDecodeError during calling form.errors with EmailField

I am porting my site to django 1.9 and don't know how to resolve this issue correctly.

In my form I have usual EmailField from django forms. If validation fails, it shoud be message about it (I pass

'form_errors': form.errors}
to context for manipulating).

But in that case django returns


DjangoUnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in
position 0: ordinal not in range(128). You passed in
()


in
django.core.validators
there is a validator for it

@deconstructible
class EmailValidator(object):
message = _('Enter a valid email address.')
...


If I change message to
message = 'error'
all works fine.

So, question: how can I fix this issue without editing django files?

the problem is in calling form.errors, this error raise even if I want only to print it. (
print form.errors
). Another fields errors (IntegerField, URLField, for example) works fine, this problem is only for EmailField.

in view process looks like that now:

from django.http import JsonResponse
...

if form.is_valid():
...
else:
return JsonResponse({'form_errors': form.errors})


last traceback is:

File "/path/views.py", line 331, in custom_form_post
response = JsonResponse({'form_errors': form.errors})
File "/path/.env/local/lib/python2.7/site-packages/django/http/response.py", line 505, in __init__
data = json.dumps(data, cls=encoder, **json_dumps_params)
File "/usr/lib/python2.7/json/__init__.py", line 250, in dumps
sort_keys=sort_keys, **kw).encode(obj)
File "/usr/lib/python2.7/json/encoder.py", line 207, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib/python2.7/json/encoder.py", line 270, in iterencode
return _iterencode(o, 0)
File "/path/.env/lib/python2.7/_abcoll.py", line 581, in __iter__
v = self[i]
File "/path/.env/local/lib/python2.7/site-packages/django/forms/utils.py", line 146, in __getitem__
return list(error)[0]
File "/path/.env/local/lib/python2.7/site-packages/django/core/exceptions.py", line 165, in __iter__
yield force_text(message)
File "/path/.env/local/lib/python2.7/site-packages/django/utils/encoding.py", line 88, in force_text
raise DjangoUnicodeDecodeError(s, *e.args)
DjangoUnicodeDecodeError: 'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128). You passed in <django.utils.functional.__proxy__ object at 0x40a6a90c> (<class 'django.utils.functional.__proxy__'>)

Answer

Well. The problem is in translations. In this case - Russian localization. No idea why translation from "native" django localization files failed. But for all who have similar problem:

  1. Create (if still not) locale file (https://docs.djangoproject.com/en/1.9/topics/i18n/translation/#localization-how-to-create-language-files)

  2. Add theese rows to django.po: msgid "Enter a valid email address." msgstr "Введите правильный адрес электронной почты." (or another translation you need)

  3. Compile (django-admin compilemessages)

Comments