38👍
Do you have this at the beginning of your script:
# -*- coding: utf-8 -*-
…?
See this: http://www.python.org/dev/peps/pep-0263/
EDIT: For the second problem, it’s about the html encoding. Put this in the head of your html page (you should send the request as an html page, otherwise I don’t think you will be able to output that character correctly):
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
17👍
Insert at the top of views.py
# -*- coding: utf-8 -*-
And add “u” before your string
my_str = u"plus de détails"
Solved!
- [Django]-Django Celery Logging Best Practice
- [Django]-Raw query must include the primary key
- [Django]-Switching from MySQL to Cassandra – Pros/Cons?
8👍
You need the coding comment Gabi mentioned and also use the unicode “u” sign before your string :
return HttpResponse(u'español')
The best page I found on the web explaining all the ASCII/Unicode mess is this one :
http://www.stereoplex.com/blog/python-unicode-and-unicodedecodeerror
Enjoy!
- [Django]-User Authentication in Django Rest Framework + Angular.js web app
- [Django]-Django urlsafe base64 decoding with decryption
- [Django]-Django models ForeignKey on_delete attribute: full meaning?
- [Django]-Django – how do I select a particular column from a model?
- [Django]-Django: import auth user to the model
- [Django]-In Django, is it possible to access the current user session from within a custom tag?
0👍
I was struggling with the same issue as @dkgirl, yet despite making all of the changes suggested here I still could not get constant strings that I’d defined in settings.py that contain ñ to show up in pages rendered from my templates.
Instead I replaced every instance of “utf-8” in my python code from the above solutions to “ISO-8859-1” (Latin-1). It works fine now.
Odd since everything seems to indicate that ñ is supported by utf-8 (and in fact I’m still using utf-8 in my templates). Perhaps this is an issue only on older Django versions? I’m running 1.2 beta 1.
Any other ideas what may have caused the problem? Here’s my old traceback:
Traceback (most recent call last):
File “manage.py”, line 4, in
import settings # Assumed to be in the same directory.
File “C:\dev\xxxxx\settings.py”, line 53
(‘es’, ugettext(u’Espa±ol’) ),
SyntaxError: (unicode error) ‘utf8’ codec can’t decode byte 0xf1 in position 0:
unexpected end of data
- [Django]-How do I use Django's logger to log a traceback when I tell it to?
- [Django]-Django: detect admin login in view or template
- [Django]-Passing data from Django to D3
0👍
ref from: https://docs.djangoproject.com/en/1.8/ref/unicode/
“If your code only uses ASCII data, it’s safe to use your normal strings, passing them around at will, because ASCII is a subset of UTF-8.
Don’t be fooled into thinking that if your DEFAULT_CHARSET setting is set to something other than ‘utf-8’ you can use that other encoding in your bytestrings! DEFAULT_CHARSET only applies to the strings generated as the result of template rendering (and email). Django will always assume UTF-8 encoding for internal bytestrings. The reason for this is that the DEFAULT_CHARSET setting is not actually under your control (if you are the application developer). It’s under the control of the person installing and using your application – and if that person chooses a different setting, your code must still continue to work. Ergo, it cannot rely on that setting.
In most cases when Django is dealing with strings, it will convert them to Unicode strings before doing anything else. So, as a general rule, if you pass in a bytestring, be prepared to receive a Unicode string back in the result.”
- [Django]-Creating a REST API for a Django application
- [Django]-Django custom managers – how do I return only objects created by the logged-in user?
- [Django]-Django: order by position ignoring NULL
0👍
The thing about encoding is that apart from declaring to use UTF-8 (via <meta>
and the project’s settings.py
file) you should of course respect your declaration: make sure your files are saved using UTF-8 encoding.
The reason is simple: you tell the interpreter to do IO using a specific charset. When you didn’t save your files with that charset, the interpreter will get lost.
Some IDEs and editors will use Latin1 (ISO-8859-1) by default, which explains why Ryan his answer could work. Although it’s not a valid solution to the original question being asked, but a quick fix.
- [Django]-ImportError: cannot import name '…' from partially initialized module '…' (most likely due to a circular import)
- [Django]-Nginx doesn't serve static
- [Django]-Map object is not JSON serializable