views:

180

answers:

1

I've got the following error:

TemplateSyntaxError at /admin/results_cop/copsegmentresult/

Caught an exception while rendering: ('ascii', 'ISU European Figure Skating Championships 2009: Senior Ladies Ladies: Short Program - 2. Susanna P\xc3\x96YKI\xc3\x96', 98, 99, 'ordinal not in range(128)')

The fragment of the string that won't render is: PÖYKIÖ

What I don't get is why Django is trying to render the string as ASCII, why not UTF-8?

EDIT 1:

I forgot to ask - I'd also quite like to know how to get rid of the error ;)

EDIT 2:

Bobince's answer is correct :) I had something along the lines of:

def __unicode__(self):
    return "%s %s" (self.foo, self.bar)
+4  A: 

I'm guessing you're asking Django to render a byte string. No u at the start of this:

'ISU European Figure Skating Championships 2009: Senior Ladies Ladies: Short Program - 2. Susanna P\xc3\x96YKI\xc3\x96'

So Django is probably trying to encode it to the page's encoding, presumably UTF-8. But byte strings can't be encoded directly; they have to be Unicode strings first. Python itself does this converting step using a default encoding which is typically ascii.

>>> 'P\xc3\x96YKI\xc3\x96'.encode('utf-8')
UnicodeDecodeError

So what you need to do is convert that byte string to a Unicode string yourself by UTF-8-decoding it, before it gets sent to the template. Where has it come from? Usually you should aim to keep all content strings inside your application as Unicode strings.

bobince
Thanks :) Changing the text to u"%s %s" did indeed solve the problem :)
Monika Sulik