0

I want to know which way is correct to serve static files in Django. All are working but which one is the best way.

I have tried {{ STATIC_URL }} in my templates. Which is working fine.

And then {% load static %} and used {% static 'path/to/static/file' %} to load static file. And this one is also working. {% load staticfiles %} and then {% static 'path/to/static/file' %} for loading static file.

I am confused which one is the correct and should be used. Please can someone explain it to me. As far as i know {{ STATIC_URL }} tag just uses the STATIC_URL defined in settings.py file.

But what does load static and load staticfiles does behind the scenes. I have tried the official documentation and this one

timo.rieber
  • 3,727
  • 3
  • 32
  • 47
Vaibhav Jain
  • 5,287
  • 10
  • 54
  • 114

1 Answers1

1

If you have any plan to use an alternative storage (for example using django-stroage), you should use {% static ... %} tag.

static tag ask storage backend to return url.

For default storage FileSystemStorage, static returns just settings.STAITC_URL joined with the request file name.


load loads custom tags, filters. To use static, you need to load it because it is not part of libraries that is loaded by default.

falsetru
  • 357,413
  • 63
  • 732
  • 636
  • @user2217267, About `load staticfiles`, read a Note in [`static` documentation](https://docs.djangoproject.com/en/1.7/ref/templates/builtins/#std:templatetag-static). In short, you need to use `staticfiles` if you want to use another backend storage backend. – falsetru Sep 27 '14 at 05:00