简体   繁体   English

UnicodeDecodeError Django Error Collectstatic

[英]UnicodeDecodeError Django Error Collectstatic

i have a problem. 我有个问题。 I use collectstatic for production. 我使用collectstatic进行生产。 But i have this problem with a css file. 但是我有一个CSS文件的问题。 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xe1 in position 240647: invalid continuation byte UnicodeDecodeError:'utf-8'编解码器无法解码位置240647中的字节0xe1:无效的继续字节

But, i don't know why. 但是,我不知道为什么。

This is the traceback: 这是回溯:

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

Thank you very much for your help! 非常感谢您的帮助!

UPDATE: 更新:

I try fix it deleting this css file, but, i deleted and i have the same error, i don't understand why! 我尝试解决此问题,删除此CSS文件,但是,我删除了,并且出现了相同的错误,我不明白为什么!

Probably your CSS file is not really encoded as UTF8, most likely it is ISO-8859-1 in which the byte 0xE1 equates to á (LATIN SMALL LETTER A WITH ACUTE). 您的CSS文件可能未真正编码为UTF8,很可能是ISO-8859-1,其中字节0xE1等于á (带小字符的拉丁文小写字母A)。 You can check the file type with the file command, and then convert it to UTF8 using iconv : 您可以使用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

Not sure about how to update the file in your installation - you said that you tried removing it without any change, so maybe you need to restart your server? 不确定如何在安装中更新文件-您说您尝试不做任何更改就删除了它,因此也许您需要重新启动服务器?

If you don't have iconv you can convert it to UTF8 in Python: 如果没有iconv ,则可以在Python中将其转换为UTF8:

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

I had the same error when I used django-pipeline inside docker container. 当我在docker容器中使用django-pipeline时,我遇到了相同的错误。 It turned out that for some reason the system used POSIX locale. 事实证明,由于某种原因,系统使用POSIX语言环境。 I used the solution proposed here and exported locale setting in system shell: 我使用了这里提出的解决方案并在系统外壳中导出了语言环境设置:

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

It worked well. 运行良好。 Also, notice I did that both in docker and outside machine. 另外,请注意我在docker和外部计算机中都这样做了。

Django has a good function for converting strings to unicode. Django具有将字符串转换为unicode的良好功能。 Try out this function 试试这个功能

from from django.utils.encoding import smart_unicode

smart_unicode(value)

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

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