如何修复Django Translation ASCII解码错误?

时间:2016-10-28 09:54:08

标签: python django unicode utf-8 ascii

我有这个django网站从locale / lc_messages / django.po和django.mo文件翻译从英语到德语。但是,当我尝试翻译像ü和ä这样的德语字母时,它会给我错误

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

据我所知,django试图通过ASCII解码unicode字符,这是不可能的。但我该如何解决这个问题呢?我应该在哪里设置django通过utf-8解码而不是通过ASCII解码。如果我错了,请告诉我。

回溯:

DjangoUnicodeDecodeError at /de/
'ascii' codec can't decode byte 0xc3 in position 7: ordinal not in range(128). You passed in <django.utils.functional.__proxy__ object at 0x0000000005EB40B8> (<class 'django.utils.functional.__proxy__'>)
Request Method: GET
Request URL:    http://127.0.0.1:8000/de/
Django Version: 1.10.1
Exception Type: DjangoUnicodeDecodeError
Exception Value:    
'ascii' codec can't decode byte 0xc3 in position 7: ordinal not in range(128). You passed in <django.utils.functional.__proxy__ object at 0x0000000005EB40B8> (<class 'django.utils.functional.__proxy__'>)
Exception Location: C:\Users\deybala1\AppData\Local\Continuum\Anaconda2\lib\site-packages\django\utils\encoding.py in force_text, line 88
Python Executable:  C:\Users\deybala1\AppData\Local\Continuum\Anaconda2\python.exe
Python Version: 2.7.11
Python Path:    
['C:\\Users\\deybala1\\Documents\\pe-locator',
 'C:\\Users\\deybala1\\AppData\\Local\\Continuum\\Anaconda2\\lib\\site-packages\\pyexcel-0.2.3-py2.7.egg',
 'C:\\Users\\deybala1\\AppData\\Local\\Continuum\\Anaconda2\\lib\\site-packages\\django_registration-2.1.2-py2.7.egg',
 'C:\\Users\\deybala1\\AppData\\Local\\Continuum\\Anaconda2\\lib\\site-packages\\django_http_proxy-0.4.3-py2.7.egg',
 'C:\\Users\\deybala1\\AppData\\Local\\Continuum\\Anaconda2\\lib\\site-packages\\django_redis-4.4.4-py2.7.egg',
 'C:\\Users\\deybala1\\AppData\\Local\\Continuum\\Anaconda2\\lib\\site-packages\\django_redis_sessions-0.5.6-py2.7.egg',
 'C:\\Users\\deybala1\\AppData\\Local\\Continuum\\Anaconda2\\python27.zip',
 'C:\\Users\\deybala1\\AppData\\Local\\Continuum\\Anaconda2\\DLLs',
 'C:\\Users\\deybala1\\AppData\\Local\\Continuum\\Anaconda2\\lib',
 'C:\\Users\\deybala1\\AppData\\Local\\Continuum\\Anaconda2\\lib\\plat-win',
 'C:\\Users\\deybala1\\AppData\\Local\\Continuum\\Anaconda2\\lib\\lib-tk',
 'C:\\Users\\deybala1\\AppData\\Local\\Continuum\\Anaconda2',
 'c:\\users\\deybala1\\appdata\\local\\continuum\\anaconda2\\lib\\site-packages\\sphinx-1.3.5-py2.7.egg',
 'c:\\users\\deybala1\\appdata\\local\\continuum\\anaconda2\\lib\\site-packages\\setuptools-20.3-py2.7.egg',
 'C:\\Users\\deybala1\\AppData\\Local\\Continuum\\Anaconda2\\lib\\site-packages',
 'C:\\Users\\deybala1\\AppData\\Local\\Continuum\\Anaconda2\\lib\\site-packages\\win32',
 'C:\\Users\\deybala1\\AppData\\Local\\Continuum\\Anaconda2\\lib\\site-packages\\win32\\lib',
 'C:\\Users\\deybala1\\AppData\\Local\\Continuum\\Anaconda2\\lib\\site-packages\\Pythonwin']
Server time:    Fr, 28 Okt 2016 04:58:23 -0500
Unicode error hint

The string that could not be encoded/decoded was: ne pr��fung

这是我的msgid和来自django.po的msgstr

#: .\app\templates\app\layout.html:47
msgid "Take Exam"
msgstr "Eine prüfung anlegen"

2 个答案:

答案 0 :(得分:3)

找到解决方案,必须将标题放在顶部的所有.po文件中。

"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"

感谢帮助人们。使用 poedit 处理.po文件

是更好的解决方案

答案 1 :(得分:0)

po文件中是否存在问题(它是python 2.7)?你可以尝试:

msgstr u"Eine prüfung anlegen"