Reputation: 37474
I'm trying to send latin characters (é, è...) to the client side using Django, and I can't get it to work. In Django I tried to write directly latin characters in my python files, but I had errors. I then used unicode (writing 'Soci\u00E9t\u00E9' for 'Société'), but when sending it to the client side I get the raw unicode characters.
Can anybody help?
Julien
Upvotes: 1
Views: 502
Reputation: 378
I would generally refrain from putting any non-ascii encoded characters in source code.
Despite the fact that the python interpreter will honor a source file with the UTF-8 header, the problem is that dumb (meaning non UTF-8 aware) text editors, shells, OSes and some developers :), will eventually corrupt your carefully crafted unicode strings.
I would stay to an ASCII only character set in any source file, since that is pretty much universally supported everywhere and thus you won't run into source code corruption.
You are almost there with your unicode escaped string.
In python you simply need to put a u in front.
i.e. u'Soci\u00E9t\u00E9'
Otherwise, python would treat most of the characters above as literal characters rather than your intended unicode escape characters.
An easy way to test the correctness of your unicode encoded string or binary string (which is a string without a u in front of it) is to fire up a python interpreter and print it:
>>> print 'Soci\u00E9t\u00E9'
Soci\u00E9t\u00E9 #ewwwwwww
>>> print u'Soci\u00E9t\u00E9'
Société #yay!!!
Note: For the above test, your shell better support UTF-8 (if you are logged in locally on a Mac it probably is)... otherwise the print output will be gibberish. That is fine, as well as it is the correct gibberish (meaning, your input string was formatted in the ideal sense).
Unless you have some middleware or other methods not properly dealing with unicode in between your view function and rendering, Django should properly output your unicode string just fine.
BTW, it is a good practice to prefix all your strings with u. u'this is a test' is just as good as 'this is a test'.
The difference is that, any string manipulation operations involving a unicode designated string (such as u'this' + ' a ' + 'test'), will tell python to be unicode aware.
Yes, python in a sense, defaults to be unicode dumb. Just like some text editors! :)
'u', the magic sauce
Upvotes: 1
Reputation: 8017
Check the file encoding for your Python files. Make sure they're UTF-8. And also, make sure that the client side is also UTF-8.
Upvotes: 0