在django 1.7中,collectstatic为我抛出了一个例外:
OSError: [Errno 2] No such file or directory: '/static'
我已经执行了很多collectstatic
- 来电,一切正常,但今天有这个问题。
settings.py
BASE_DIR = os.path.dirname(os.path.realpath(__file__))
INSTALLED_APPS = (
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'fxblog',
'rest_framework',
)
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, STATIC_URL.strip("/"))
STATICFILES_DIRS = (
'/static/',
'/upload/',
)
BASE_DIR是正确的,检查一下。目录BASE_DIR / static / exists以及我的所有静态文件都在那里。
回溯:
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 399, in execute_from_command_line
utility.execute()
File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 392, 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 242, in run_from_argv
self.execute(*args, **options.__dict__)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 285, in execute
output = self.handle(*args, **options)
File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 415, in handle
return self.handle_noargs(**options)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 173, in handle_noargs
collected = self.collect()
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/management/commands/collectstatic.py", line 103, 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 106, 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 25, in get_files
directories, files = storage.listdir(location)
File "/usr/local/lib/python2.7/dist-packages/django/core/files/storage.py", line 249, in listdir
for entry in os.listdir(path):
OSError: [Errno 2] No such file or directory: '/static'
有什么建议吗?
答案 0 :(得分:14)
试试这个:
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
并将其留空,因为您使用的是STATIC_ROOT
STATICFILES_DIRS = (
# Put strings here, like "/home/html/static" or "C:/www/django/static".
# Always use forward slashes, even on Windows.
# Don't forget to use absolute paths, not relative paths.
)
它应该以这种方式工作。
答案 1 :(得分:5)
STATICFILES_DIRS中的文件需要具有绝对路径。 使用normpath。
STATICFILES_DIRS = (
normpath(join(BASE_DIR, 'static')),
normpath(join(BASE_DIR, 'upload')),
)
最好将STATIC_ROOT设置为
STATIC_ROOT = normpath(join(BASE_DIR, 'assets'))
和STATIC_URL
STATIC_URL = '/static/'
答案 2 :(得分:1)
所以我遇到了类似的问题,并按照 fraggles 的建议,但我得到了这个跟进错误:
NameError: name 'normpath' is not defined
我的工作是切换&#39; .dirname&#39; &#39; .normpath&#39;的关键字改为关键字:
STATICFILES_DIRS = (
os.path.join(os.path.normpath(BASE_DIR), "static")
)
它就像魔法一样。希望这个解决方案也可以帮助某人。