UnicodeEncodeError: 'ascii' codec can't encode character UnicodeEncodeError: 'ascii' codec can't encode character django django

UnicodeEncodeError: 'ascii' codec can't encode character


For anyone encountering this problem when running Django with Supervisor, the solution is to add e.g. the following to the supervisord section of Supervisor's configuration:

environment=LANG="en_US.utf8", LC_ALL="en_US.UTF-8", LC_LANG="en_US.UTF-8"

This solved the problem for me in Supervisor 3.0a8 running on Debian Squeeze.

Also make sure Supervisor re-reads the configuration by running:

supervisorctl rereadsupervisorctl restart myservice

(thanks @Udi)


For upstart, add in your /etc/init/myservice.conf:

env LANG="en_US.utf8"env LC_ALL="en_US.UTF-8"env LC_LANG="en_US.UTF-8"`

(thanks @Andrii Zarubin; see Environment Variables in Upstart documentation for more information)


In situations where you must display a unicode string in a place that only accepts ascii (like the console or as a path) you must tell Python that you want it to replace the non ascii characters best effort.

>> problem_str = u'This is not all ascii\xf8 man'>> safe_str = problem_str.encode('ascii', 'ignore')>> safe_str'This is not all ascii man'

Encoding issues are prevented in the admin by the cautious handing of Django templating, but if you have ever added custom columns and forgotten to convert the values to ascii, or you override the str method for a model and forget to do this, you will get the same error, preventing template rendering.

If this string were saved into your (hopefully utf8) database there would be no problem, it looks like you are trying to upload a file that uses the title of an entity that has a non ascii character.


Hope this would help.In my case, I'm running django through daemontools.

Setting

export LANG='en_US.UTF-8'export LC_ALL='en_US.UTF-8'

in run script before executing manage.py resolved the issue with uploads filename