Alex Rivera | Logout

Django: Non-ASCII character

Asked 2011-01-08T17:34:43.613
22

My Django View/Template is not able to handle special characters. The simple view below fails because of the ñ. I get below error:

Non-ASCII character '\xf1' in file"

def test(request):
    return HttpResponse('español')

Is there some general setting that I need to set? It would be weird if I had to handle all strings separately: non-American letters are pretty common!

EDIT This is in response to the comments below. It still fails :(

I added the coding comment to my view and the meta info to my html, as suggested by Gabi.

Now my example above doesn't give an error, but the ñ is displayed incorrectly.

I tried return render_to_response('tube/mysite.html', {"s": 'español'}). No error, but it doesn't dislay (it does if s = hello). The other information on the html page displays fine.

I tried hardcoding 'español' into my HTML and that fails:

UnicodeDecodeError 'utf8' codec can't decode byte 0xf.

I tried with the u in front of the string:

SyntaxError (unicode error) 'utf8' codec can't decode byte 0xf1

Does this help at all??

Edit
Report

1 Answer

3

Set DEFAULT_CHARSET to 'utf-8' in your settings.py file.

answered 2011-01-09T11:34:38.683

Your Answer