20

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 processor.

Can someone explain what the difference between them is, and any advantages there might be to using on over the other.

thomas
  • 949
  • 6
  • 20

1 Answers1

39

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.

Thomas Orozco
  • 53,284
  • 11
  • 113
  • 116