okrutny
okrutny

Reputation: 1110

Locale on django and uwsgi UnicodeEncodeError

EDIT: I just realized, that when i'm not trying to print to console that variable, it works. Why?

I run into an issue related to displaying string label with utf chars. I set locale env in uwsgi ini file like this:

env =LC_ALL=en_US.UTF-8
env =LANG=en_US.UTF-8

and in wsgi.py:

locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')

When I run app code:

print (locale.getlocale(), locale.getpreferredencoding())
print locale.getdefaultlocale()
print "option_value", option_value
label = force_text(option_label)
print 'label', label #THIS FAILS

the output is:

(('en_US', 'UTF-8'), 'UTF-8')
('en_US', 'UTF-8')
option_value d
ERROR <stack trace>
print 'label', label
UnicodeEncodeError: 'ascii' codec can't encode character u'\u015b' in position 5: ordinal not in range(128)

The problem is not present when I run app via runserver in production environment. Django 1.6.5 Python 2.7.6 Ubuntu 14.04 uWSGI 2.0.5.1

Upvotes: 5

Views: 3076

Answers (2)

Kasravnd
Kasravnd

Reputation: 107297

for all in django when you want use unicode , like in forms and etc .. you must set a u in leading of your unicode that you want to be saved ! do this any where that your unicode have been saved ! in this case i think it is (option_label)

Upvotes: -1

okrutny
okrutny

Reputation: 1110

I just found answer here: http://chase-seibert.github.io/blog/2014/01/12/python-unicode-console-output.html

Realized that the console is responsible for that error, so exporting additional env variable in uwsgi config file solves the issue: env = PYTHONIOENCODING=UTF-8

Upvotes: 5

Related Questions