I’ve been working with international websites lately. Got this error on the Django admin pages, but no other pages on the website:

DjangoUnicodeDecodeError: Caught an exception while rendering: ‘ascii’ codec can’t decode byte … in position:6: ordinal not in range (128) ….

Went through the following steps:

  1. Is my input in utf-8? Yes.
  2. Is the database properly storing the right encoding? Yes.
  3. Is Django pulling the data out in utf-8? Yes.

Strange, this means the data is in the right encoding, but when it is trying to display on the admin page the encoding is getting lost or coerced in to the wrong thing.

After a cup of coffee, I remembered the admin pages sometimes use the unicode() function if you don’t explicitly tell them what to display. Looked into the model code and gasped in horror:

def __unicode__(self):
    return str(self.title)+u":"

Flip the str to unicode and admin pages work again.

Gotta add that to the code review list.