Whats the difference between using {{STATIC_URL}} and {% static %}

后端 未结 1 1438
没有蜡笔的小新
没有蜡笔的小新 2020-12-15 18:39

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

相关标签:
1条回答
  • 2020-12-15 19:33

    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.

    0 讨论(0)
提交回复
热议问题