108

The most important part of the question is in the topic.

I am wondering what tag is best for which case. Moreover... I found code, that also use settings.STATIC_URL included by {{STATIC_URL}} in the templates.

I am a little confused.

trikoder_beta
  • 2,800
  • 4
  • 15
  • 17

5 Answers5

62

The built-in static template tag "link[s] to static files that are saved in STATIC_ROOT".

The staticfiles contrib app's static template tag "uses the configured STATICFILES_STORAGE storage to create the full URL for the given relative path", which is "especially useful when using a non-local storage backend to deploy files".

The built-in static template tag's documentation (linked to above) has a note that says to use the staticfiles contrib app's static template tag "if you have an advanced use case such as using a cloud service to serve static files", and it gives this example of doing so:

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

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

Nick
  • 8,049
  • 18
  • 63
  • 107
  • 40
    [Django V1.10](https://docs.djangoproject.com/en/1.10/ref/templates/builtins/#static) now recommends just `{% load static %}`. "In older versions, you had to use `{% load static from staticfiles %}` in your template to serve files from the storage defined in STATICFILES_STORAGE. This is no longer required." – John C Oct 26 '16 at 13:26
  • 2
    Since 2016 we only need to use `{% load static %}`. – Uri Aug 05 '19 at 16:54
5

I don't know what the difference is supposed to be, but I found a use case difference (using django 1.9.1 running via apache, wsgi on Python 3.4). In my app, I have some images in ImageFields in the database. If I use code like this in my template:

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

then, if I use {% load static %}, django throws a TypeError (Cannot mix str and non-str arguments). This is presumably because the object.image is not a string, it's an ImageField, that gets converted to a string at some later stage. However, if one uses {% load staticfiles %} no such error occurs.

Unfortunately, I discovered this difference after spending hours trying to debug the problem. I managed to find a workaround for when using the first option, namely to add a string-converter method to the object like this:

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

Hope this knowledge will be of use to someone.

MackM
  • 2,906
  • 5
  • 31
  • 45
CoderGuy123
  • 6,219
  • 5
  • 59
  • 89
4

Django documentation prefers now {% load static %}.

{% load staticfiles %} works but I think it is deprecated.

https://docs.djangoproject.com/en/dev/ref/templates/builtins/#static

Uri
  • 2,992
  • 8
  • 43
  • 86
1

Refer to the docs, where there is a nice explanation of it. Actually the {% static %} template tag know the location of STATICFILE_STORAGE

As 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 a non-local storage backend to deploy files as documented in Serving static files from a cloud service or CDN.

If you’d like to retrieve 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 that helps!!

S.Ali
  • 664
  • 1
  • 5
  • 12
  • 19
    I still don't know when should I use `{% load static %}`, `{% load staticfiles %}`, `{{STATIC_URL}}`... and know I don't know what is the difference between `{% load static %}` and `{% load static from staticfiles %}` – trikoder_beta Jun 16 '14 at 08:51
  • 3
    simply copying a bunch of lines from the doc doesn't really help – QuestionEverything Nov 09 '17 at 22:26
1

{% load staticfiles %} is very helpful when you are using different storages like S3, then it will convert into the S3 URLs

Dilraj
  • 2,791
  • 2
  • 15
  • 9