1

I have a django 1.5, python 2.7 app in openshift and i'm getting a DjangoUnicodeDecodeError with non-ascii characters, like ç,á,ã..

I've spent some hours trying to fix this (i'm begginer in python, django and openshift)

It is not happening locally, just in the openshift cloud server.

Is there any way to fix this via ssh? Or any other..

Here is the printstack:

Request Method: POST

Request URL: ----

Django Version: 1.5

Exception Type: DjangoUnicodeDecodeError

Exception Value:
'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128). You passed in <django.utils.functional.proxy object at 0x7f10ccfde9d0> ()
Exception Location: /var/lib/openshift/.../python/virtenv/lib/python2.7/site-packages/Django-1.5-py2.7.egg/django/utils/encoding.py in force_text, line 115

Python Executable: /var/lib/openshift/.../python//virtenv/bin/python

Python Version: 2.7.5

Python Path:...

Thanks,

Roberto.

1

1 Answer 1

1

I had similar issues recently on my machine, I solved by using unicode literals + smart_text:

from __future__ import unicode_literals
from django.utils.encoding import smart_text


safeText = smart_text('this is my tetxt : %s' % someVaribleHoldingTextData)

alternatively you may need to decode data read from a file to a specific charset:

theFile = open(path, 'r')
safeData = theFile.read().decode('utf-8')

encoding is an hard topic… you have to try, and try again :P

Sign up to request clarification or add additional context in comments.

5 Comments

Ok, i'm gonna try it now and come back soon.. thanks
cool! I'm happy you fixed the problem so fast, I lost a whole day to fix the problem by myself some days ago :D
I saw now that the same error occurs with e-mails sending.. do you know if is there another aproach for this case?
smart_text calls force_text internally… you could inspect django sources :)
django 1.4.1 don't have smart_text in django.utils.encoding yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.