运行Django开发服务器时出现UnicodeDecodeError
UnicodeDecodeError
这个错误出现在你试图访问网站媒体文件夹里的任何文件时。
在调试模式下会显示完整的错误追踪信息:
Environment:
Request Method: GET
Request URL: http://127.0.0.1:8000/site-media/img/image.png
Django Version: 1.2.3
Python Version: 2.7.0
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'django.contrib.markup',
'special']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware')
Traceback:
File "C:\Languages\Python27\lib\site-packages\django\core\handlers\base.py" in get_response
100. response = callback(request, *callback_args, **callback_kwargs)
File "C:\Languages\Python27\lib\site-packages\django\views\static.py" in serve
59. mimetype = mimetypes.guess_type(fullpath)[0] or 'application/octet-stream'
File "C:\Languages\Python27\lib\mimetypes.py" in guess_type
294. init()
File "C:\Languages\Python27\lib\mimetypes.py" in init
355. db.read_windows_registry()
File "C:\Languages\Python27\lib\mimetypes.py" in read_windows_registry
260. for ctype in enum_types(mimedb):
File "C:\Languages\Python27\lib\mimetypes.py" in enum_types
250. ctype = ctype.encode(default_encoding) # omit in 3.x!
Exception Type: UnicodeDecodeError at /site-media/img/image.png
Exception Value: 'ascii' codec can't decode byte 0xe0 in position 0: ordinal not in range(128)
1 个回答
5
看起来这是Python 2.7的mimetypes.py文件中的一个问题。可以看看下面的讨论串:
http://groups.google.com/group/django-users/browse_thread/thread/613909b35a7462a0
这里有一篇俄文文章的链接,翻译过来是:
标准库的存在让我们很快找到了问题。Windows XP。我正在使用Windows XP。 [HKEY_CLASSES_ROOT\CLSID{4063BE15-3B08-470D-A0D5-B37161CFFD69}\EnableFullPage\MIME] 在我的情况下,问题出在注册表中 [HKEY_CLASSES_ROOT \ CLSID \ {4063BE15-3B08-470D-A0D5-B37161CFFD69} \ EnableFullPage \ MIME]下有一些子项 包含了西里尔字母的名称。如果你删除这些 键,它们会自动重新创建。因此,我通过 重命名来解决了这个问题,只是把西里尔字母 替换成了拉丁字母。