{% load static %} and {% load staticfiles %}: which is preferred?
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 :)
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 wherestatic
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
.