Django 创建了 staticfiles 文件夹而不是 static 文件夹

发布于 2025-01-16 08:11:11 字数 642 浏览 3 评论 0原文

我不知道为什么 Django 创建了一个名为 staticfiles 的文件夹,而不是预期的 static 。这可能是我在运行 python manage.pycollectstatic 后出现错误的原因:

The system cannot find the path specified: 'D:...\\static'

我的设置文件包括:

from pathlib import Path
import os
import django_heroku

BASE_DIR = Path(__file__).resolve().parent.parent

STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

我已经尝试将 STATIC_ROOT 中的“staticfiles”更改为“static”,但没有成功。

谁能解释一下为什么以及如何创建一个名为“static”的文件夹?

预先非常感谢您!

I do not know why Django created a folder named staticfiles rather than static as expected. That might be the reason why I got an error after running python manage.py collectstatic:

The system cannot find the path specified: 'D:...\\static'

My settings file included:

from pathlib import Path
import os
import django_heroku

BASE_DIR = Path(__file__).resolve().parent.parent

STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

I had already tried to change 'staticfiles' to 'static' in STATIC_ROOT, but no success.

Could anyone please explain why and how to create a folder name "static"?

Thank you very much in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

地狱即天堂 2025-01-23 08:11:11

我认为您的静态文件放置在您的应用程序中

尝试删除STATICFILES_DIRS,以便设置为:

STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'

...并尝试重新运行python管理.pycollectstatic

  • 以下是 collectstatic 的工作原理:
    • 首先,它会从 STATICFILES_DIRS 收集静态文件(如果有),如果设置中没有 STATICFILES_DIRS,它会从每个应用收集静态文件
    • 然后将它们放置到 STATIC_ROOT 中,因此,如果您的静态文件放置在应用内,最好删除 STATICFILES_DIRS

如果仍然存在错误,请分享您的项目结构

I considered your static files are placed inside your app(s)

Try removing STATICFILES_DIRS so the setting will be:

STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'

...and try to re-run python manage.py collectstatic

  • Here is how collectstatic works:
    • First it collect static files from STATICFILES_DIRS if any, if there is no STATICFILES_DIRS in settings it collects static files from each app
    • Then placed them to STATIC_ROOT, so if your static files are placed inside your app(s) better to remove STATICFILES_DIRS

If still there is an error share your project structure

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文