UnicodeEncodeError in django app
I'm trying to create an app using Django on webfaction. I basically was messing around with the Amazon API, and when one of the search results has a trademark symbol, which is passed to my template...the error is thrown. I'm getting the error
开发者_如何转开发Caught UnicodeEncodeError while rendering: 'ascii' codec can't encode character u'\u2122' in position 9: ordinal not in range(128)
and was wondering if anyone knew what the fix is.
It probably means you are calling str()
on a a peice of unicode data - the str
function could be called ascii
to better describe what it does! Your templates will be totally happy with unicode data so given that you are using Django I suspect the problem is in a __unicode__
method or some such.
Unicode is a tricky subject, have a Google for python unicode
to get a feel for it.
Tricky to help you further without seeing some more code but the gist is to try and use unicode strings all through your application! Python has a unicode()
method that works exactly like the str
method for simple strings but will work fine with unicode strings as well - it's better to use that.
Yes, u'\u2122'
is the trade mark sign. Somewhere in your code, you should be:
- encoding your unicode data using a codec (utf8, cp1250 to cp1258, etc) that supports that character
or
- avoiding an automatic unexpected decoding (which uses ascii, which doesn't support that character).
Which action is needed and where? No idea, as you haven't supplied a traceback ... please edit your question to include the full traceback, and format it as code, so that it's legible.
精彩评论