Django 和客户端之间的编码问题
我正在尝试使用 Django 将拉丁字符 (é、è...) 发送到客户端,但无法让它工作。在 Django 中,我尝试在 python 文件中直接写入拉丁字符,但出现错误。然后我使用了 unicode(将“Société”写为“Soci\u00E9t\u00E9”),但是当将其发送到客户端时,我得到了原始的 unicode 字符。
有人可以帮忙吗?
朱利安
I'm trying to send latin characters (é, è...) to the client side using Django, and I can't get it to work. In Django I tried to write directly latin characters in my python files, but I had errors. I then used unicode (writing 'Soci\u00E9t\u00E9' for 'Société'), but when sending it to the client side I get the raw unicode characters.
Can anybody help?
Julien
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通常会避免在源代码中放入任何非 ASCII 编码的字符。
尽管 python 解释器将尊重带有 UTF-8 标头的源文件,但问题是愚蠢的(意思是不支持 UTF-8 的)文本编辑器、shell、操作系统和一些开发人员:)最终会破坏您精心制作的内容统一码字符串。
我会在任何源文件中保留仅 ASCII 字符集,因为这几乎在所有地方都得到普遍支持,因此您不会遇到源代码损坏的情况。
你的 unicode 转义字符串就快到了。
在Python中你只需要把au放在前面。
IE
u'Soci\u00E9t\u00E9'
否则,python 会将上面的大多数字符视为文字字符,而不是您想要的 unicode 转义字符。
测试 unicode 编码字符串或二进制字符串(前面没有 au 的字符串)正确性的一个简单方法是启动 python 解释器并打印它:
注意:对于上述测试,您的 shell 更好地支持 UTF -8(如果您在 Mac 上本地登录,则可能是)...否则打印输出将是乱码。这很好,而且它是正确的乱码(也就是说,您的输入字符串的格式是理想的)。
除非你有一些中间件或其他方法无法在视图函数和渲染之间正确处理 unicode,否则 Django 应该正确输出你的 unicode 字符串。
顺便说一句,在所有字符串中添加 u 前缀是一个很好的做法。
“这是一个测试”与“这是一个测试”一样好。
不同之处在于,任何涉及 unicode 指定字符串的字符串操作操作(例如 u'this' + ' a' + 'test')都会告诉 python 识别 unicode。
是的,从某种意义上说,python 默认是 unicode 哑的。就像一些文本编辑器一样! :)
“u”,神奇酱料
I would generally refrain from putting any non-ascii encoded characters in source code.
Despite the fact that the python interpreter will honor a source file with the UTF-8 header, the problem is that dumb (meaning non UTF-8 aware) text editors, shells, OSes and some developers :), will eventually corrupt your carefully crafted unicode strings.
I would stay to an ASCII only character set in any source file, since that is pretty much universally supported everywhere and thus you won't run into source code corruption.
You are almost there with your unicode escaped string.
In python you simply need to put a u in front.
i.e.
u'Soci\u00E9t\u00E9'
Otherwise, python would treat most of the characters above as literal characters rather than your intended unicode escape characters.
An easy way to test the correctness of your unicode encoded string or binary string (which is a string without a u in front of it) is to fire up a python interpreter and print it:
Note: For the above test, your shell better support UTF-8 (if you are logged in locally on a Mac it probably is)... otherwise the print output will be gibberish. That is fine, as well as it is the correct gibberish (meaning, your input string was formatted in the ideal sense).
Unless you have some middleware or other methods not properly dealing with unicode in between your view function and rendering, Django should properly output your unicode string just fine.
BTW, it is a good practice to prefix all your strings with u.
u'this is a test' is just as good as 'this is a test'.
The difference is that, any string manipulation operations involving a unicode designated string (such as u'this' + ' a ' + 'test'), will tell python to be unicode aware.
Yes, python in a sense, defaults to be unicode dumb. Just like some text editors! :)
'u', the magic sauce
检查 Python 文件的文件编码。确保它们是 UTF-8。另外,请确保客户端也是 UTF-8。
Check the file encoding for your Python files. Make sure they're UTF-8. And also, make sure that the client side is also UTF-8.