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

标签 python django static

我使用 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/

相关文章:

python - subprocess.Popen() IO 重定向

c++ - 后期构造静态成员变量

java - 多模块项目 : reuse static method between test classes from different modules

java - 添加到静态成员字段

python - 在 Windows XP 32 位上运行的最新版本的 Python 3 和 PyQt5

python - Django 测试用例来测试模型失败

python - 在 Python 中,为什么 [2] 小于 (1,)?

python - Django DLL加载失败: The specified module could not be found

python - 如何访问 Django 中基于类的 View 中的多对多字段?

python - 将 Django 组合框选项设置为颜色?