UnicodeDecodeError: 'ascii' codec can't decode error when using render_to_response and dajax.assign

746 views Asked by At

I am new at using django and dajax and I'm having a problem using dajax.assign to load a html file into a div in another html when using non-English characters, when I set the site language to English it works fine, but when I switch to Spanish it does not.

Here is what I am doing:

I tried :

dajax.assign('#setttabs-1','innerHTML','abc')

and it works fine. The div shows the text correctly, but when I try (I am trying to do a translation of the site):

dajax.assign('#setttabs-1','innerHTML','ábcñ')

it generates an exception:

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

I went and did some searching on google and found this:

dajax.assign('#setttabs-1','innerHTML','ábcñ'.decode('utf-8'))

Which again makes it work correctly and it shows all the characters correctly, the problem is that I need to load a template in said div, so the following does not work:

dajax.assign('#setttabs-1','innerHTML',render_to_response('./settings/_change_language.html'.decode('utf-8'), context_instance=RequestContext(request)))

It also throws the exception mentioned above when it tries to load the template. The template has the encoding meta:

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

My ajax.py also starts with:

# coding: utf-8

How do I tell dajax to treat this file as utf-8 so it can render it correctly? Is there a more practical way to do what I am trying to do here? I feel like I'm missing something really simple, but I can't figure it out. Thanks in advance for any help you can provide.

1

There are 1 answers

0
Ger.Mora On

I changed:

dajax.assign('#setttabs-1','innerHTML',render_to_response('./settings/_change_language.html'), context_instance=RequestContext(request)))

to:

dajax.assign('#setttabs-1','innerHTML',render_to_string('./settings/_change_language.html'.decode('utf-8'), context_instance=RequestContext(request)))

and now it works perfectly. Don't know why though. Hope this will help someone out there.