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

I'm not sure what the difference is, they both seem to work. I googled around, and it seems like it's almost the same thing. Just out of curiosity, what kind of person is using the field?

I read this, but still do not know when to use, which and what kind of people in the field are using. Mine works for both of them. At first I thought that it loads a static folder, but it works for static files too ... -

+12
django django-templates django-staticfiles
Dec 22 '15 at 7:19
source share
2 answers

Currently (Django 1.9 and earlier) {% load staticfiles %} loading the templateetag static from the Contrib application, which has more features than the built-in django.core.static .

The most important difference is that staticfiles can manage files stored on a CDN, since its recognizer can manage hashes, for example. core.static only adds STATIC_URL to the static file name, which is not enough if you are processing your files (for example, adding an hdd hash to clear the cache between releases)

This difference is due to the fact that managing non-local storage files was not intended to be included in the main Django package, but is still useful for many developers who need to be implemented as an official contribution package. Therefore, if you started using staticfiles , you had to remember to use it each in your templates. BUT, there may be some problems, for example, if using the Media classes , so it was decided to combine these two templatetags into one and use the other developer behavior django.contrib.staticfiles in his INSTALLED_APPS or not.

From Django 1.10 onwards (also see the Django tracker ticket ), {% load static %} will use staticfiles internally if it is activated (otherwise follow the default behavior), and the templatetag in the contrib package will become obsolete to avoid confusion.

TL; DR

  • Prior to Django 1.10 : staticfiles loads templatetags, which can manage non-local storage, where static cannot (or not easily);
  • From Django 1.10 : contrib.staticfiles application still exists, but its templatetags will only be removed {% static %} templatetags remains;
  • From Django 2.0 (I believe): {% load staticfiles %} has been removed.

Currently use staticfiles templatetags if you use the appropriate Contrib application (and know why you use it) before Django 1.10, otherwise just use static .

+24
Dec 22 '15 at 20:33
source share

just an interesting piece of code inside '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 I can’t assume that {% load staticfiles%} will be deleted after django 3 is released :)

+1
Aug 23 '19 at 21:33
source share



All Articles