【问题标题】:Django : execute command collectstatic raise UnicodeDecodeErrorDjango:执行命令 collectstatic 引发 UnicodeDecodeError
【发布时间】:2012-08-08 13:38:31
【问题描述】:

为什么要引发 UnicodeDecodeError? 我尝试使用 apache 部署我的 django 应用程序 复制静态文件,输入

$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)

我的静态文件有什么问题? 我的设置.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 主机配置

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

【问题讨论】:

  • PROJECT_ROOT的值是多少;例如你的项目的路径?它包含一个非 ASCII 字符。
  • @MartijnPieters 这是我的项目名称。并且它不包含非 ASCII 字符。我将其更改为 '/srv/www/yyy/static/' 但错误继续引发。
  • 嗯,这是您的项目名称加上绝对路径。但是您将其设置为另一条路径的测试表明,下一个合乎逻辑的步骤是检查静态文件本身。然后那里的东西包含一个非ASCII字符。

标签: python django apache static


【解决方案1】:

看起来要复制的静态文件的一个或多个路径包含非 ASCII 字符。

它与destination目录的路径无关。

找出答案的一种方法是放入

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

就在 /usr/local/lib/python2.7/dist-packages/django/core/files/storage.py 的第 236 行之前片刻,然后再次运行 manage.py。

然后你应该看到问题发生在哪里(你不会看到真正的罪魁祸首,而是在它之前的文件和问题文件的适当目录)。

或者,您也可以使用 pdb

python -m pdb manage.py collectstatic

并在调试器中检查导致问题的文件。

【讨论】:

  • 我知道这是一个老问题,但第 236 行是什么?我现在也面临同样的问题。
  • 您实际上可以在问题中显示的堆栈跟踪中看到它 - 调用 os 包函数,该函数传递了一个 str,然后无法使用 ascii 解码器将其解码为 Unicode。
【解决方案2】:

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

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

您可以在之后检查您的语言环境应该是这样的:

vagrant@vagrant-ubuntu-trusty-64:/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 和外部机器中都这样做了。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-10-13
    • 2018-09-09
    • 1970-01-01
    • 2021-01-29
    • 2012-12-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多