Skip to content Skip to sidebar Skip to footer

Whats The Difference Between Using {{static_url}} And {% Static %}

Throughout the django documentation and a lot of tutorials people seem to pick freely between using the {% static %} tag, and using {{ STATIC_URL }} with the correct context proces

Solution 1:

Abstract

The {% static %} template tag is aware of your STATICFILES_STORAGE, using the STATIC_URL setting is not.

Rule of thumb

Use the template tag.

Manually concatenating is bad practice ("do I need a slash?"), and will eventually bite you, generally when you decide to change static files storage.

Examples

Authenticated URLs

Here's an example. You might want to use AWS S3 for static files hosting, all the while not making your files public. You'll then be serving those using AWS S3 authenticated URLS.

The correct URL will look something like:

 https://s3.amazonaws.com/bucket/file.ext?signature=1234

The {% static %} template tag will let you add the signature. Using STATIC_URL will not.

Fingerprinted URLs

In a similar fashion, if your static files storage fingerprints your files, using STATIC_URL will not work.

Post a Comment for "Whats The Difference Between Using {{static_url}} And {% Static %}"