Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
337 views
in Technique[技术] by (71.8m points)

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...