Django’s dev server and UTF-8
From time to time I got this UnicodeEncodeError, but I had done all the things (sitecustomize.py, some more) right in order to configure the system in UTF-8. I thought.
I did the usual
./manage runserver
and since I assumed that on my Mac everything is already properly configured to be UTF-8 I didn’t even think of the shell not being properly set up. Fortunately my colleague Tobi found out that I have to set the shell variable LANG=”utf-8″. So now I always do:
export LANG="utf-8"
./manage runserver
So I went from the wrong locale output to the one working properly with UTF-8 and Django, jiha!
cain@home:~> locale LANG= ... cain@home:~> export LANG="utf-8" cain@home:~> locale LANG="utf-8" ... cain@home:~> # now i can safely start Djangocain@home:~> ./manage runserver
ephemient said,
November 23, 2007 at 1:56 pm
‘LANG=utf-8′ is not a valid system locale.
Choose from the output of `locale -a`; en_US.UTF-8 is a reasonable choice.
What distro are you using? Both Fedora and Ubuntu set globally this by default.
Wolfram said,
November 23, 2007 at 2:53 pm
I got a Mac, thats the diff.
Chris Ryland said,
November 23, 2007 at 5:01 pm
Well, at least on Leopard, LANG=en_US.UTF-8 by default. (That’s what my shell is showing, and I know I never set it anywhere.)
Wolfram said,
November 23, 2007 at 5:56 pm
Good to know. So it seems before Leopard the default was not set to utf-8. (I dont remember at least having unset it :-))
ephemient said,
November 24, 2007 at 6:29 am
Ah, right. I used to have a Mac OS X 10.3 machine, and I remember being annoyed at LANG/LC_* environment variables not being set by default. It doesn’t matter to me anymore, since I upgraded the OS to Linux instead of 10.4, but it’s nice to know that they fixed that.
Niran said,
November 24, 2007 at 11:56 am
If you ever write Python code that you intend to distribute to other people, or even other servers you control that haven’t been set up like yours has, you can’t write code that assumes that UTF-8 is the default encoding. From what I’ve read, the best option is to stick with the default (ASCII) so you know your code will work everywhere without needing to tweak anything.
Wolfram said,
November 26, 2007 at 4:07 pm
But we Germans might have stuff like Umlauts, etc
ASCII just doesn’t cover it all
jonn3 said,
May 20, 2011 at 1:01 am
comment5, 1, 15449, 3, 4688, 2, 8-]]], 2, qqv, 3, %P, 3, 30487, 1, 2523, 2, 0666, 2, urpyu, 2, 43049, 3, lkf, 3, %P, 1, >:-D, 1, 594599, 2, 8(((, 1, 93373, 1, mrpgvt, 1, %]], 2, 9235, 2, ifeb, 2, >:-O, 2, 54476, 3, %[[, 1, 8))), 2, pdodtp, 2, 85875, 2, hdsz, 2, 0685, 3, 552430, 1, xrr, 1, 8((, 1, 9790, 1, 318, 2, :-))), 1, %((, 1, 83941, 1, nznubv, 2, 904, 3, gozzzd, 2, 07279, 2, =-)), 2, rwjrb, 1, zbpt, 1, kvxzz, 2, niq, 3, 492, 2, 593, 3, 178637, 1, 198327, 2, >:-[,