本文介绍了{%load staticfiles%}与{%load static%}之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题的最重要的部分是在主题中。

The most important part of the question is in the topic.

我想知道什么标签最适合哪种情况。此外...我发现代码,也使用 settings.STATIC_URL 包含在 {{STATIC_URL}} 中的模板

I am wondering what tag is best for which case. Moreover... I found code, that also use settings.STATIC_URL included by {{STATIC_URL}} in the templates.

我有点困惑。

推荐答案

链接到保存在 STATIC_ROOT 中的静态文件。

The built-in static template tag "link[s] to static files that are saved in STATIC_ROOT".

使用配置的 STATICFILES_STORAGE 存储创建给定相对路径的完整URL,这是本地存储后端部署文件。

The staticfiles contrib app's static template tag "uses the configured STATICFILES_STORAGE storage to create the full URL for the given relative path", which is "especially useful when using a non-local storage backend to deploy files".

内置的 static 模板标签的文档(链接到上面)具有说明使用 staticfiles contrib应用程序的静态 tem如果您有高级使用案例(如使用云服务来提供静态文件),那么这个例子就是这样做的:

The built-in static template tag's documentation (linked to above) has a note that says to use the staticfiles contrib app's static template tag "if you have an advanced use case such as using a cloud service to serve static files", and it gives this example of doing so:

{% load static from staticfiles %}
<img src="{% static "images/hi.jpg" %}" alt="Hi!" />

您可以使用 {%load staticfiles%} 而不是 {%load static from staticfiles%} ,如果你愿意,但后者更加明确。

You could use {% load staticfiles %} rather than {% load static from staticfiles %} if you want, but the latter is more explicit.

这篇关于{%load staticfiles%}与{%load static%}之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 17:53