0

When

locale.getlocale()
locale.getdefaultlocale()
sys.getfilesystemencoding()
sys.getdefaultencoding()

is run through manage.py shell, I get

('en_US', 'UTF-8')
('en_US', 'UTF-8')
utf-8
utf-8

Which fits my locales:

LANG=en_US.UTF-8
LANGUAGE=en_US:
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=en_US.UTF-8

However, when the request is handled by Apache/Nginx it produces:

(None, None)
(None, None)
ascii
utf-8

This leads to several UnicodeDecodeErrors throughout my site and I failed to find the reason for the mismatch. Neither setting AddDefaultCharset utf-8 for Apache or charset utf-8; for Nginx solved the issue.

3
  • 2
    The webserver is run as a separate user. You'll have set that user's locale. A more robust approach would be not to rely on the locale anyway; declare encoding explicitly when opening files, de-/encoding streams etc. In each case ask yourself who should control the choice of encoding: the environment (possibly causing encoding errors at runtime if the environment's choices are unwise), or your interface? Commented Dec 16, 2017 at 19:23
  • I will try de-/encoding by hand. Still, Apache and Nginx are run by www-data, which has the same locales. Commented Dec 17, 2017 at 21:49
  • Sounds odd. I have to admit, though, that I don't understand every detail of how Python arrives at its default encodings for I/O. Which for me has been just another reason not to rely on them. Commented Dec 17, 2017 at 22:02

1 Answer 1

2

The solution was hidden in /etc/apache2/envvars:

## The locale used by some modules like mod_dav
export LANG=C
## Uncomment the following line to use the system default locale instead:
. /etc/default/locale
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.