为什么会引发 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
最佳答案
看起来要复制的静态文件的一个或多个路径包含非 ASCII 字符。
与目标目录的路径无关。
找出答案的一种方法是放入
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
并检查哪个文件导致调试器中出现问题。
关于python - Django : execute command collectstatic raise UnicodeDecodeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11915432/