[Django]-Why does Django admin try to encode strings into ASCII rather than Unicode? Or is this error something different than it looks like?

7👍

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.

3👍

If anyone is confused by bobince’s answer, think of this:

The fields from the model are already in unicode format.

when you have a unicode function like this:

def __unicode__(self):
  return "{0}".format(self.field_one)

It is actually returning a ASCII string(which means, it will try to convert field_one to ASCII), if the field_one contains characters outside of ASCII, you will get the problem as above.

Now consider this unicode function:

def __unicode__(self):
      return self.field_one

This works fine, because you are returning unicode string directly, no conversion needed.

Lets revisit the first unicode function, to make it work, you just need to add u to make it a unicode string

def __unicode__(self):
      return u"{0}".format(self.field_one)

Leave a comment