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

11,396

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

Solution 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 :)

Share:
11,396
Admin
Author by

Admin

Updated on June 06, 2022

Comments

  • Admin
    Admin almost 2 years

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

  • Uri
    Uri almost 5 years
    Since 2016 we only need to use {% load static %}.
  • Uri
    Uri almost 5 years