Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
340 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 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...