`FileNotFoundError`:当发出`python manage.py collectstatic`时

时间:2018-05-23 12:35:12

标签: django

当我尝试发出命令来收集静态文件时

FileNotFoundError:

抛出FileNotFoundError: [Errno 2] No such file or directory: '/Users/me/Desktop/Django/forum/static'

In [44]: ! tree -L 2
.
├── db.sqlite3
├── forum
│   ├── __init__.py
│   ├── __pycache__
│   ├── settings.py
│   ├── static
│   ├── templates
│   ├── urls.py
│   └── wsgi.py
├── forums
│   ├── __init__.py
│   ├── __pycache__
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   ├── models.py
│   ├── static
│   ├── templates
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── ll_forum
│   ├── bin
│   ├── include
│   ├── lib
│   ├── pip-selfcheck.json
│   ├── pyvenv.cfg
│   └── share
└── manage.py

14 directories, 15 files

然而,存在文件' / Users / me / Desktop / Django / forum / static'

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    #my apps
    "forums"
]
STATIC_URL = '/static/'
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"), #notice the comma
)

setting.py

%MARGINAL
(DSN=cabg /* Name of original dataset */ 
,PARMS=parms_OpDeath /* Parameter estimates from model */ 
,KEY=medRecN /* Key */ 
,DEPENDENT=yom /* Dependent variable */ 
,INDEPENDENT=age_n /* Independent variables listed */ 
,CLASS=GENDER /* Class variable for marginal effects */ 
,NAME_1=%bquote(1, Male) /* User-supplied name of first data element*/ 
,NAME_2=%bquote(2, Female) /* User-supplied name of second data element*/ 
);

2 个答案:

答案 0 :(得分:1)

你应该在你的设置中提供STATIC_ROOT,默认情况下,Django会在根项目中查找名为static的目录,这就是为什么你得到这个/Users/me/Desktop/Django/forum/static'``FileNotFoundError文件夹的原因不存在。

您的设置旁边有static目录,它不应该在那里。顺便说一下,你的模板也不应该在那里。

将它们移回根项目

├── db.sqlite3
├── forum
│   ├── __init__.py
│   ├── __pycache__
│   ├── settings.py
|   |---static # remove this
│   ├── templates # remove this here
│   ├── urls.py
│   └── wsgi.py
|---static -- # GOOD
|---templates # GOOD

答案 1 :(得分:1)

您需要在Django App外部拥有静态目录。这个目录和你的Django应用程序应该是分开的。

然后,如果您指定:

STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"), #notice the comma
)

这意味着您的静态目录位于基本目录中。或者您只有forumforumsll_forum ...

如果你想绝对保留你的Django App中的静态目录,在BASE_DIR中创建这个目录(从初始位置移动到你的基础项目)并在settings.py文件中写这样的东西:

STATIC_URL = '/static/'
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static"),]

PROJECT_DIR = os.path.dirname(os.path.abspath(__file__))
STATIC_ROOT = os.path.join(PROJECT_DIR, 'static/')

最后,你应该有这样的事情:

.
├── db.sqlite3
├── forum
│   ├── __init__.py
│   ├── __pycache__
│   ├── settings.py
│   ├── static
│   ├── templates
│   ├── urls.py
│   └── wsgi.py
├── forums
│   ├── __init__.py
│   ├── __pycache__
│   ├── admin.py
│   ├── apps.py
│   ├── migrations
│   ├── models.py
│   ├── tests.py
│   ├── urls.py
│   └── views.py
├── ll_forum
│   ├── bin
│   ├── include
│   ├── lib
│   ├── pip-selfcheck.json
│   ├── pyvenv.cfg
│   └── share
└── manage.py
│
└── static
│
└── templates
相关问题