2014-09-23 38 views
7

在Django 1.7 collectstatic抛出一个异常,对我来说:Django的collectstatic没有这样的文件或目录

OSError: [Errno 2] No such file or directory: '/static' 

我已经进行了很多collectstatic -calls,一切工作正常,但今天有这个问题。

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 /静态/存在,我所有的静态文件都在那里。

回溯:

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

'STATIC_ROOT = os.path.join(BASE_DIR,'sitestatic')' – 2014-09-23 09:08:02

+0

同样的输出 - 'OSError:[Errno 2]没有这样的文件或目录:'/ static''but directory(in the output)changed to sitestatic。 – 2014-09-23 09:11:47

+0

这个怎么样? ''STATIC_ROOT = os.path.join(BASE_DIR,'/ static')''。这应该工作。 ''''strip''去掉''/''但它需要''/ static'' – doniyor 2014-09-23 09:15:59

回答

4

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/' 
+1

也尝试过你的解决方案 - 它的工作,也谢谢。 – 2014-09-23 09:39:34

11

试试这个:

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

它应该这样工作。

+0

试过你的解决方案 - 它的工作,谢谢。 – 2014-09-23 09:38:32

+1

不幸的是,我已经试过@ fragles的解决方案,所以我只能upvote你的答案。 – 2014-09-23 09:40:11

+1

谢谢,这对我有用:) – valkirilov 2016-12-11 14:52:52

0

所以我有类似的问题,其次是什么fraggles所建议的,但我得到这个跟进错误:

NameError: name 'normpath' is not defined 

我的工作是围绕为切换” .normpath的“.dirname”关键字'关键字代替:

STATICFILES_DIRS = (
    os.path.join(os.path.normpath(BASE_DIR), "static") 
) 

它的工作方式像魔术一样。希望这个解决方案也可以帮助某人。

相关问题