What is the difference between {% load staticfiles%} and {% load static%}

The most important part of the question in the topic.

I am wondering which tag is best suited for this case. Moreover ... I found code that also uses settings.STATIC_URL included {{STATIC_URL}} in the templates.

I am a bit confused.

+47
django django-templates
Jun 16 '14 at 7:24
source share
4 answers

Built-in static template tag "link [s] to static files stored in STATIC_ROOT ".

staticfiles contrib app static template tag "uses the configured STATICFILES_STORAGE repository to create the full URL for this relative path", which is "especially useful when using non-local repository to deploy files."

The built-in documentation for the static template tag (linked above) has a note that says to use the staticfiles contrib app static template tag β€œif you have an extended use case, such as using a cloud service to serve static files,” and it gives the following example:

 {% load static from staticfiles %} <img src="{% static "images/hi.jpg" %}" alt="Hi!" /> 

You can use {% load staticfiles %} rather than {% load static from staticfiles %} if you want, but the latter is more explicit.

+39
Dec 17 '14 at 0:01
source share

I don’t know what the difference should be, but I found the difference in use (using django 1.9.1 running via apache, wsgi on Python 3.4). In my application, I have several images in ImageFields in the database. If I use this code in my template:

 <a href="object-{{object.id}}"><img src="{% static object.image %}" height="200px"></a> 

then if I use {% load static %} , django will have a TypeError value ( Cannot mix str and non-str arguments ). This is probably due to the fact that object.image not a string, but ImageField , which is converted to a string at a later stage. However, if you use {% load staticfiles %} , this error does not occur.

Unfortunately, I found this difference by spending hours debugging the problem. I managed to find a workaround for using the first option, namely to add the string converter method for the object as follows:

 #image string def image_str(self): return str(self.image) 

We hope that this knowledge will be useful for someone.

+3
Jan 27 '16 at 19:56
source share

Refer to the docs for a good explanation. In fact, the template tag {% static %} knows the location of STATICFILE_STORAGE

As the docs say:

  {% load static from staticfiles %} <img src="{% static "images/hi.jpg" %}" alt="Hi!" /> The previous example is equal to calling the url method of an instance of STATICFILES_STORAGE with "images/hi.jpg". 

This is especially useful when using non-local storage to deploy files, as described in Serving Static Files from a Service Cloud or CDN.

If you want to get a static URL without displaying it, you can use a slightly different call:

 {% load static from staticfiles %} {% static "images/hi.jpg" as myphoto %} <img src="{{ myphoto }}" alt="Hi!" /> 

Hope this helps!

+1
Jun 16 '14 at 7:40
source share

{% load staticfiles %} very useful when you use different repositories like S3 and then convert to S3 URLs

+1
Mar 22 '16 at 18:12
source share



All Articles