0

I am getting the error

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 138: ordinal not in range(128)

when I try to do

from django.utils.encoding import smart_str

u'{}'.format(smart_str('ø'))

but the documentation says that the default encoding for this function is utf-8 which I thought should include 'ø'. I am using Django 1.11 on Python 2.7. What am I missing here?

wogsland
  • 9,106
  • 19
  • 57
  • 93

1 Answers1

0

So reading this post I discovered that Python 2.7 (but not Python 3.x) has issues converting between utf-8 and unicode. Who'da thunk it? So I needed to decode my string before formatting it:

u'{}'.format(smart_str('ø').decode('utf-8'))
wogsland
  • 9,106
  • 19
  • 57
  • 93