1

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.

John Machin
  • 81,303
  • 11
  • 141
  • 189
iman453
  • 9,105
  • 16
  • 54
  • 70

2 Answers2

3

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.

adamnfish
  • 10,935
  • 4
  • 31
  • 40
1

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.

John Machin
  • 81,303
  • 11
  • 141
  • 189