gpt4 book ai didi

python - 当你没有在 Django 中定义 STATICFILES_DIR 时会发生什么?

转载 作者:行者123 更新时间:2023-11-28 22:40:26 25 4
gpt4 key购买 nike

我使用 django 并且我的应用程序可以正常工作,当我更改 style.css 时应用程序会发生变化等。

但是最近发现我没有定义STATICFILES_DIR,我只定义了

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

仅适用于 collectstatic

但现在我想知道,django 是如何知道去哪里寻找静态文件的?

具体在polls/static/polls目录下?

最佳答案

默认情况下,Django 会在您的每个应用程序中查找名为 static 的目录。稍后会详细介绍。

来自documentation :

Now we might be able to get away with putting our static files directly in my_app/static/ (rather than creating another my_app subdirectory), but it would actually be a bad idea. Django will use the first static file it finds whose name matches, and if you had a static file with the same name in a different application, Django would be unable to distinguish between them.

关键部分是

...Django will use the first static file it finds whose name matches...

有称为“静态文件查找器”的 Django 库可以帮助完成此过程。您可以通过修改 Django 配置文件中已安装的应用程序来配置它们。

根据 the documentation ,它们实际上没有记录,因为它们被认为是私有(private)的。但是,您可以 look at the source code对于 GitHub 上的那些“发现者”。

Django 项目由称为应用程序的模块组成。每个应用程序在生成时都包含一些默认文件。虽然有配置,但也有很多关于 Django 的约定。例如,views.py 文件。另一个这样的约定是您应用程序的静态目录文件夹。

STATICFILES_DIR 数组允许您添加额外的位置来放置静态文件。由于 Django 在真实环境中处理静态文件的方式,这很有用,您将在准备部署时看到这一点。

它完全是可选的,但它可以让您以后的生活更轻松。

关于python - 当你没有在 Django 中定义 STATICFILES_DIR 时会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33655791/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com