Vaibhav Jain
Vaibhav Jain

Reputation: 5507

Serve static files in Django. Need explanation

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

Upvotes: 0

Views: 74

Answers (1)

falsetru
falsetru

Reputation: 368904

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.

Upvotes: 1

Related Questions