Encoding is, along with Regular Expressions, one of those subjects I never seem to properly learn. Here are a few pointers to my future self whenever I run into a pesky UnicodeEncodeError.
- Read this article for the umtillionth time. This might be the time it "clicks" for you.
- Don't get fancy with ISO-8859-1. Just use UTF-8. App Engine hates everything that is UTF-8.
- Make sure your text editor saves the files using UTF-8 encoding.
- Make sure that the
- Put # coding=UTF-8 at the top of every .py file
- Put a u in front of every string that contains non-ascii chars. Like this u"Jag är en liten hatt och är bög"
- When handling incoming requests, make sure you set the encoding correctly using request.encoding = "UTF-8" in your view
- Use ugettext as your alias for _
- If a method (such as quote() or hashlib.sha224() requires bytestrings as input, encode your unicode strings first - like this: theunicodestring.encode("utf-8")