2012-08-11 64 views
1

为什么引发UnicodeDecodeError? 我尝试使用Apache 复制静态文件部署我的Django应用程序,输入Django:执行命令collectstatic引发UnicodeDecodeError

$python manage.py collectstatic 

,我得到错误信息象下面。

You have requested to collect static files at the destination 
location as specified in your settings. 

This will overwrite existing files! 
Are you sure you want to do this? 

Type 'yes' to continue, or 'no' to cancel: yes 
Traceback (most recent call last): 
File "manage.py", line 10, in <module> 
execute_from_command_line(sys.argv) 
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 443, in execute_from_command_line 
utility.execute() 
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 382, in execute 
self.fetch_command(subcommand).run_from_argv(self.argv) 
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 196, in run_from_argv 
self.execute(*args, **options.__dict__) 
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 232, in execute 
output = self.handle(*args, **options) 
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 371, in handle 
return self.handle_noargs(**options) 
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 163, in handle_noargs 
collected = self.collect() 
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 104, in collect 
for path, storage in finder.list(self.ignore_patterns): 
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/finders.py", line 137, in list 
for path in utils.get_files(storage, ignore_patterns): 
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/utils.py", line 37, in get_files 
for fn in get_files(storage, ignore_patterns, dir): 
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/utils.py", line 37, in get_files 
for fn in get_files(storage, ignore_patterns, dir): 
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/utils.py", line 25, in get_files 
directories, files = storage.listdir(location) 
File "/usr/local/lib/python2.7/dist-packages/django/core/files/storage.py", line 236, in listdir 
if os.path.isdir(os.path.join(path, entry)): 
File "/usr/lib/python2.7/posixpath.py", line 71, in join 
path += '/' + b 
UnicodeDecodeError: 'ascii' codec can't decode byte 0xba in position 1: ordinal not in range(128) 

我的静态文件有什么问题? 我settings.py

import os 
PROJECT_ROOT = os.path.dirname(__file__) 
STATIC_ROOT = os.path.join(PROJECT_ROOT, 'static/') 
# URL prefix for static files. 
# Example: "http://media.lawrence.com/static/" 
STATIC_URL = '/static/' 


STATICFILES_FINDERS = (
'django.contrib.staticfiles.finders.FileSystemFinder', 
'django.contrib.staticfiles.finders.AppDirectoriesFinder', 
# 'django.contrib.staticfiles.finders.DefaultStorageFinder', 
) 

和Apache主机的conf

ServerName www.abcd.org 
DocumentRoot /srv/www/yyy 

<Directory /srv/www/yyy> 
    Order allow,deny 
    Allow from all 
</Directory> 

WSGIDaemonProcess yyy.djangoserver processes=2 threads=15 display-name=%{GROUP} 
WSGIProcessGroup iii.djangoserver 

WSGIScriptAlias//srv/www/yyy/apache/django.wsgi 

+0

'PROJECT_ROOT'的价值是什么;例如您的项目的路径?它包含一个非ASCII字符。 – 2012-08-11 14:20:52

+0

@MartijnPieters这是我的项目名称。它不包含非ASCII字符。我通过'/ srv/www/yyy/static /'改变了它,但是错误继续增加。 – 2012-08-11 15:40:22

+0

嗯,这是你的项目名称*加上它的绝对路径*。但是,将其设置为另一个路径的测试表明,下一个逻辑步骤是检查静态文件本身。那里的东西然后包含一个非ASCII字符。 – 2012-08-11 19:29:10

回答

3

看起来像一个或多个路径,以你的静态文件,这些文件将被复制包含非ASCII字符。

它与desctination目录的路径无关。

一个办法,找出是

try: 
    print path 
except: 
    pass 
try: 
    print entry 
except: 
    pass 

之前线236 /usr/local/lib/python2.7/dist-packages/django/core/files/storage。 py一会儿,然后再次运行manage.py。

然后你应该看到问题出现在哪里(你不会看到罪魁祸首,而是它之前的文件,并有问题的文件的propably目录)。

,或者,你可以使用PDB

python -m pdb manage.py collectstatic 

,并检查该文件引起的调试器的问题。

+0

我知道这是一个老问题,但在236行是什么?目前我面临同样的问题。 – 2017-07-20 07:24:47

+0

您可以在问题中显示的堆栈跟踪中实际看到它 - 调用os包函数,该函数传递了一个str,然后无法使用ascii解码器将其解码为Unicode。 – zifot 2017-07-20 07:51:13

+0

是的,谢谢!我在看之前问过......抱歉 – 2017-07-20 07:52:45

0

我在Docker容器中使用了django-pipeline时发生了同样的错误。事实证明,由于某些原因,系统使用POSIX语言环境。我这里使用的建议,并在系统shell出口区域设置了解决方案:

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

您可以检查后您的区域应该是这样的:

[email protected]:/project$ locale 
LANG=en_US.UTF-8 
LANGUAGE= 
LC_CTYPE="en_US.UTF-8" 
LC_NUMERIC="en_US.UTF-8" 
LC_TIME="en_US.UTF-8" 
LC_COLLATE="en_US.UTF-8" 
LC_MONETARY="en_US.UTF-8" 
LC_MESSAGES="en_US.UTF-8" 
LC_PAPER="en_US.UTF-8" 
LC_NAME="en_US.UTF-8" 
LC_ADDRESS="en_US.UTF-8" 
LC_TELEPHONE="en_US.UTF-8" 
LC_MEASUREMENT="en_US.UTF-8" 
LC_IDENTIFICATION="en_US.UTF-8" 
LC_ALL=en_US.UTF-8 

它运作良好。另外,请注意我在Docker和外部机器中都这样做了。