{% load static %} 和 {% load staticfiles %}:哪个是首选? [英] {% load static %} and {% load staticfiles %}: which is preferred?

查看:57
本文介绍了{% load static %} 和 {% load staticfiles %}:哪个是首选?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定有什么区别,看起来它们都在工作.我用谷歌搜索,似乎它们几乎是一样的.出于好奇,人们在该领域使用哪一种?

I'm not sure what the difference is, it seems like both of them are working. I googled around, and seems like they are pretty much same thing. Just out of curiosity, which one do people use in the field?

我读过,但仍然不知道何时使用哪个,以及该领域的人使用哪个.我的作品对他们两个都有效.起初我以为它正在加载静态文件夹,但它也适用于静态文件...... –

I read that but still don't know when to use which, and which one people in the field use. Mine works for both of them. At first I thought it's loading static folder but it works for staticfiles too... –

推荐答案

目前(Django 1.9 及更早版本),{% load staticfiles %} 加载 static来自 contrib 应用程序的模板标签,它比内置的 django.core.static 具有更多的功能.

For the moment (Django 1.9 and earlier), {% load staticfiles %} loads the static templatetag from the contrib app that has more features than the built-in django.core.static.

最重要的区别是 staticfiles 可以管理存储在 CDN 上的文件,因为它的解析器可以管理例如哈希值.core.static 仅将 STATIC_URL 附加到静态文件名,如果您正在处理文件,这还不够(例如,添加 md5 哈希以清除版本之间的缓存)

The most important difference is staticfiles can manage files stored on CDN, since its resolver can manage hashes for example. core.static only append STATIC_URL to the static filename, which is not enough if you're processing your files (e.g. adding md5 hash to clear cache between releases)

这种差异是因为管理非本地存储文件并不是专门包含在 Django 的核心包中,但作为官方的 contrib 包实现对许多开发人员仍然有用.所以如果你开始使用 staticfiles,你必须记住在你的模板中使用它.但是,可能会出现一些问题,例如当 使用 Media 所以决定是将这两个模板标签合并为一个,并使用不同的行为,无论开发者在其 INSTALLED_APPS 中是否有 django.contrib.staticfiles.

This difference is due to the fact that managing non-local storage files was not dedicated to be included in the core package of Django, but was still useful to many developers to be implemented as a official contrib package. So if you started to use staticfiles, you had to remember to use it every in your templates. BUT, some problems could appear, for example when using Media classes so the decision has been to merge those two templatetags into one and use a different behaviour whether the developer has django.contrib.staticfiles in its INSTALLED_APPS or not.

来自 Django 1.10 及更高版本(也查看 Django 跟踪器中的票),{% load static %}将在内部使用 staticfiles 如果被激活(否则保持默认行为),并且 contrib 包中的模板标签将被弃用以避免混淆.

From Django 1.10 and onwards (also see ticket in Django tracker), the {% load static %} is going to use staticfiles internally if activated (oherwise keep default behaviour), and the templatetag in the contrib package will be deprecated to avoid confusion.

TL;DR

  • Django 1.10 之前:staticfiles 加载模板标签,可以管理 static 不能(或不容易)的非本地存储;
  • 从 Django 1.10: contrib.staticfiles 应用程序仍然存在,但它的模板标签将被删除,只有 {% static %} 模板标签仍然存在;
  • 从 Django 2.0(我相信):{% load staticfiles %} 被删除.
  • Before Django 1.10: staticfiles loads a templatetags that can manage non-local storage where static can't (or not easily) ;
  • From Django 1.10: contrib.staticfiles app still exist but its templatetags will be removed only the {% static %} templatetags remains ;
  • From Django 2.0 (I believe): {% load staticfiles %} is removed.

目前,如果您在 Django 1.10 之前使用相关的 contrib 应用程序(并且您知道使用它的原因),请使用 staticfiles 模板标签,否则只需使用 static.

For now, use staticfiles templatetags if you use the related contrib app (and you know why you are using it) until Django 1.10, otherwise just use static.

这篇关于{% load static %} 和 {% load staticfiles %}:哪个是首选?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆