【问题标题】:`FileNotFoundError`: when issue `python manage.py collectstatic``FileNotFoundError`:当发出`python manage.py collectstatic`
【发布时间】:2018-11-02 10:56:36
【问题描述】:

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

In [41]: ! python manage.py collectstatic

它抛出FileNotFoundError:

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

然而,存在文件'/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

设置.py

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
)

【问题讨论】:

  • @JuniorGantin 根据他的问题,这不是一个好的答案。
  • 请注意,在将项目部署到生产环境之前,您不必设置 STATIC_ROOT 或运行 collectstatic。由于您的INSTALLED_APPS 中有'django.contrib.staticfiles',因此runserver 将负责为您提供静态文件。

标签: django


【解决方案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

【讨论】:

  • 但为什么官方教程建议在应用程序中包含模板和静态?
  • 你可以这样做,但是在你放的地方,那个导演根本不是一个应用程序......应用程序是由这个命令创建的那些manage.py startapp app_name
  • 我现在明白了。
【解决方案2】:

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

那么,如果你指定:

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

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

如果你想在你的 Django 应用程序中绝对保留你的静态目录,在 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

【讨论】:

  • 官方教程建议在应用中包含模板和静态。
猜你喜欢
  • 2018-02-19
  • 2020-12-05
  • 1970-01-01
  • 2016-02-01
  • 1970-01-01
  • 2020-06-13
  • 2021-05-01
  • 2017-05-21
  • 2019-08-27
相关资源
最近更新 更多