{% load static %} and {% load staticfiles %}: which is preferred?

左心房为你撑大大i 提交于 2019-11-27 15:06:44

问题


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... –


回答1:


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.

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)

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.

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

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

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.




回答2:


just an interesting piece of code inside the 'django/contrib/staticfiles/templatetags/staticfiles.py' about this topic:

import warnings

from django import template
from django.templatetags.static import (
    do_static as _do_static, static as _static,
)
from django.utils.deprecation import RemovedInDjango30Warning

register = template.Library()


def static(path):
    warnings.warn(
        'django.contrib.staticfiles.templatetags.static() is deprecated in '
        'favor of django.templatetags.static.static().',
        RemovedInDjango30Warning,
        stacklevel=2,
    )
    return _static(path)


@register.tag('static')
def do_static(parser, token):
    warnings.warn(
        '{% load staticfiles %} is deprecated in favor of {% load static %}.',
        RemovedInDjango30Warning,
    )
    return _do_static(parser, token)

so ill dare to assume {% load staticfiles %} is going to be removed after the django 3 release :)



来源:https://stackoverflow.com/questions/34422971/load-static-and-load-staticfiles-which-is-preferred

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!