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

查看:4319
本文介绍了{%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?

我读了,但仍然不知道什么时候使用哪一个,哪一个人在该领域使用。我的工作都是两个人。起初我以为它正在加载静态文件夹,但它也适用于staticfiles ... -

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%} 加载了比具有更多功能的contrib应用程序的 static templatetag -in 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 ,你必须记住在你的模板中使用它。但是,可能会出现一些问题,例如使用媒体 classes ,所以决定是将这两个模板集合合并成一个,并使用不同的行为,无论开发人员是否在其中使用了 django.contrib.staticfiles INSTALLED_APPS

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 internal 如果激活(oherwise保持默认行为),并且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%} templatetags保留;

  • 从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.

使用 staticfiles templatetags,如果您使用相关的contrib应用程序(并且您知道为什么使用它),直到Django 1.10,否则只需使用 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天全站免登陆