Pergunta

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.

Foi útil?

Solução

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

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top