UnicodeDecodeError Django错误Collectstatic

时间:2014-07-16 00:15:12

标签: python django deployment django-staticfiles

我有一个问题。 我使用collectstatic进行生产。 但我有一个css文件的问题。 UnicodeDecodeError:' utf-8'编解码器不能解码240647位的字节0xe1:无效的连续字节

但是,我不知道为什么。

这是追溯:

Post-processed 'tiempo_turco/stylesheets/foundation.css' as 'tiempo_turco/stylesheets/foundation.6f8a1d5c4dbc.css'
Traceback (most recent call last):
File "./manage.py", line 10, in <module>
execute_from_command_line(sys.argv)
File "/home/tulipan/Proyectos/IspanyolHaber/lib/python3.4/site-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
utility.execute()
File "/home/tulipan/Proyectos/IspanyolHaber/lib/python3.4/site-packages/django/core/management/__init__.py", line 392, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/home/tulipan/Proyectos/IspanyolHaber/lib/python3.4/site-packages/django/core/management/base.py", line 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/home/tulipan/Proyectos/IspanyolHaber/lib/python3.4/site-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "/home/tulipan/Proyectos/IspanyolHaber/lib/python3.4/site-packages/django/core/management/base.py", line 415, in handle
return self.handle_noargs(**options)
File "/home/tulipan/Proyectos/IspanyolHaber/lib/python3.4/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 173, in handle_noargs
collected = self.collect()
File "/home/tulipan/Proyectos/IspanyolHaber/lib/python3.4/site-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 119, in collect
for original_path, processed_path, processed in processor:
File "/home/tulipan/Proyectos/IspanyolHaber/lib/python3.4/site-packages/django/contrib/staticfiles/storage.py", line 251, in post_process
content = original_file.read().decode(settings.FILE_CHARSET)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe1 in position 240647: invalid continuation byte

非常感谢你的帮助!

更新:

我尝试修复它删除此css文件,但是,我删除了,我有同样的错误,我不明白为什么!

3 个答案:

答案 0 :(得分:1)

可能你的CSS文件并没有真正编码为UTF8,很可能是ISO-8859-1,其中字节0xE1等于á(LATIN SMALL LETTER A WITH ACUTE)。您可以使用file命令检查文件类型,然后使用iconv将其转换为UTF8:

$ cp tiempo_turco/stylesheets/foundation.css /tmp
$ file /tmp/foundation.css
/tmp/foundation.css: ISO-8859 text
$ iconv -f ISO-8859-1 -t UTF8  /tmp/foundation.css >/tmp/foundation_utf8.css
$ file /tmp/foundation_utf8.css
/tmp/foundation_utf8.css: UTF-8 Unicode text

不确定如何更新安装中的文件 - 您说您尝试删除它而不做任何更改,那么您是否需要重新启动服务器?

如果您没有iconv,可以在Python中将其转换为UTF8:

$ python
>>> css = open('/tmp/foundation.css').read().decode('iso-8859-1')
>>> open('/tmp/foundation_utf8.css', 'w').write(css.encode('utf8'))

答案 1 :(得分:1)

当我在docker容器中使用 django-pipeline 时,我遇到了同样的错误。事实证明,由于某种原因,系统使用POSIX语言环境。我使用了提议的解决方案here并在系统shell中导出了区域设置:

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8

效果很好。另外,请注意我在docker和外部机器上都这样做了。

答案 2 :(得分:0)

Django具有将字符串转换为unicode的良好功能。试试这个功能

from from django.utils.encoding import smart_unicode

smart_unicode(value)