首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >“`FileNotFoundError`”:发出“`FileNotFoundError`”时

“`FileNotFoundError`”:发出“`FileNotFoundError`”时
EN

Stack Overflow用户
提问于 2018-05-23 12:35:12
回答 2查看 2.1K关注 0票数 0

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

代码语言:javascript
复制
In [41]: ! python manage.py collectstatic

它抛出FileNotFoundError:

代码语言:javascript
复制
FileNotFoundError: [Errno 2] No such file or directory: 
'/Users/me/Desktop/Django/forum/static'

尽管如此,仍然存在文件‘/Users/me/Desktop/Django/论坛/静态’。

代码语言:javascript
复制
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

The setting.py

代码语言:javascript
复制
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
)
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-05-23 12:43:00

您应该在设置中提供STATIC_ROOT,默认情况下Django在根项目中查找名为static的目录,这就是为什么您的get文件夹不存在的原因。

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

将它们移回根项目。

代码语言:javascript
复制
├── db.sqlite3
├── forum
│   ├── __init__.py
│   ├── __pycache__
│   ├── settings.py
|   |---static # remove this
│   ├── templates # remove this here
│   ├── urls.py
│   └── wsgi.py
|---static -- # GOOD
|---templates # GOOD
票数 1
EN

Stack Overflow用户

发布于 2018-05-23 12:44:38

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

然后,如果您指定:

代码语言:javascript
复制
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"), #notice the comma
)

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

如果您想绝对地将静态目录保存在Django应用程序中,请在BASE_DIR中创建这个目录(从初始位置移动到基本项目),并在settings.py文件中编写如下内容:

代码语言:javascript
复制
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/')

最后,您应该有这样的东西:

代码语言:javascript
复制
.
├── 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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50488586

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档