简体   繁体   English

Django:非ASCII字符

[英]Django: Non-ASCII character

My Django View/Template is not able to handle special characters. 我的Django视图/模板无法处理特殊字符。 The simple view below fails because of the ñ. 由于ñ,下面的简单视图失败了。 I get below error: 我得到以下错误:

Non-ASCII character '\\xf1' in file" 文件中的非ASCII字符'\\ xf1'

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. 我按照Gabi的建议将编码注释添加到我的视图中,并将元信息添加到我的html中。

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'}) . 我试过return render_to_response('tube/mysite.html', {"s": 'español'}) No error, but it doesn't dislay (it does if s = hello). 没有错误,但是没有错误(如果s = hello则会发生)。 The other information on the html page displays fine. html页面上的其他信息显示正常。

I tried hardcoding 'español' into my HTML and that fails: 我尝试将'español'硬编码到我的HTML中,但失败了:

UnicodeDecodeError 'utf8' codec can't decode byte 0xf. UnicodeDecodeError'utf8'编解码器无法解码字节0xf。

I tried with the u in front of the string: 我在字符串前面尝试了你:

SyntaxError (unicode error) 'utf8' codec can't decode byte 0xf1 SyntaxError(unicode错误)'utf8'编解码器无法解码字节0xf1

Does this help at all?? 这有帮助吗?

Do you have this at the beginning of your script: 你在脚本的开头是否有这个:

# -*- coding: utf-8 -*-

...? ...?

See this: http://www.python.org/dev/peps/pep-0263/ 见: http//www.python.org/dev/peps/pep-0263/

EDIT : For the second problem, it's about the html encoding. 编辑 :对于第二个问题,它是关于HTML编码。 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): 把它放在你的html页面的头部(你应该把请求作为html页面发送,否则我认为你不能正确输出该字符):

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

Insert at the top of views.py 插入views.py的顶部

# -*- coding: utf-8 -*-

And add "u" before your string 并在字符串前添加“u”

my_str = u"plus de détails"

Solved! 解决了!

You need the coding comment Gabi mentioned and also use the unicode "u" sign before your string : 您需要Gabi提到的编码注释,并在字符串前使用unicode“u”符号:

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 我在网上找到的解释所有ASCII / Unicode混乱的最佳页面是: http//www.stereoplex.com/blog/python-unicode-and-unicodedecodeerror

Enjoy! 请享用!

settings.py文件中将DEFAULT_CHARSET设置为'utf-8'

ref from: https://docs.djangoproject.com/en/1.8/ref/unicode/ ref来自: 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. “如果您的代码只使用ASCII数据,那么使用普通字符串是可以安全的,可以随意传递它们,因为ASCII是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设置被设置为'utf-8'之外的其他东西,你可以在你的字节串中使用其他编码! DEFAULT_CHARSET only applies to the strings generated as the result of template rendering (and email). DEFAULT_CHARSET仅适用于作为模板呈现(和电子邮件)结果生成的字符串。 Django will always assume UTF-8 encoding for internal bytestrings. Django将始终假定内部字节串的UTF-8编码。 The reason for this is that the DEFAULT_CHARSET setting is not actually under your control (if you are the application developer). 原因是DEFAULT_CHARSET设置实际上并不在您的控制之下(如果您是应用程序开发人员)。 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. 在大多数情况下,当Django处理字符串时,它会在执行任何其他操作之前将它们转换为Unicode字符串。 So, as a general rule, if you pass in a bytestring, be prepared to receive a Unicode string back in the result." 因此,作为一般规则,如果传入一个bytestring,请准备好在结果中接收一个Unicode字符串。“

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. 关于编码的事情是,除了声明使用UTF-8(通过<meta>和项目的settings.py文件)之外,您当然应该尊重您的声明: 确保使用UTF-8编码保存您的文件。

The reason is simple: you tell the interpreter to do IO using a specific charset. 原因很简单:您告诉解释器使用特定的字符集进行IO。 When you didn't save your files with that charset, the interpreter will get lost. 当您没有使用该charset保存文件时,解释器将丢失。

Some IDEs and editors will use Latin1 (ISO-8859-1) by default, which explains why Ryan his answer could work. 一些IDE和编辑器默认使用Latin1(ISO-8859-1),这解释了为什么Ryan他的答案可行。 Although it's not a valid solution to the original question being asked, but a quick fix. 虽然它不是原始问题的有效解决方案,但是快速修复。

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. 我正在努力解决与@dkgirl相同的问题,但尽管在此处提出了所有更改,我仍然无法获得我在settings.py中定义的包含ñ的常量字符串,以显示在从我的模板呈现的页面中。

Instead I replaced every instance of "utf-8" in my python code from the above solutions to " ISO-8859-1 " (Latin-1). 相反,我将我的python代码中的每个“utf-8”实例从上面的解决方案替换为“ 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). 奇怪,因为一切似乎表明ñ是由utf-8支持的(实际上我仍然在我的模板中使用utf-8)。 Perhaps this is an issue only on older Django versions? 也许这只是旧版Django版本的问题? I'm running 1.2 beta 1. 我正在运行1.2 beta 1。

Any other ideas what may have caused the problem? 可能导致问题的其他任何想法? Here's my old traceback: 这是我的旧追溯:
Traceback (most recent call last): Traceback(最近一次调用最后一次):
File "manage.py", line 4, in 文件“manage.py”,第4行,in
import settings # Assumed to be in the same directory. 导入设置#假设在同一目录中。
File "C:\\dev\\xxxxx\\settings.py", line 53 文件“C:\\ dev \\ xxxxx \\ settings.py”,第53行
('es', ugettext(u'Espa±ol') ), ('es',ugettext(u'Espa±ol')),
SyntaxError: (unicode error) 'utf8' codec can't decode byte 0xf1 in position 0: unexpected end of data SyntaxError :( unicode错误)'utf8'编解码器无法解码位置0的字节0xf1:意外的数据结束

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM