使用{{STATIC_URL}}与{%static%}之间有什么区别? [英] Whats the difference between using {{STATIC_URL}} and {% static %}

查看:705
本文介绍了使用{{STATIC_URL}}与{%static%}之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在整个django文档和许多教程中,人们似乎可以在使用 {%static%} 标记之间自由选择,并使用 {{ STATIC_URL}} 使用正确的上下文处理器。



有人可以解释它们之间的区别,以及可能使用的任何优点

解决方案

摘要



{%static%} 模板标签是 STATIC_URL 设置不了解 STATICFILES_STORAGE



经验法则



使用模板标签。



手动连接是不好的做法(我需要斜杠吗?),最终会咬你,一般来说,当你决定更改静态文件存储。



示例



经过身份验证的网址



例。您可能希望将AWS S3用于静态文件托管,同时不会将您的文件公开。然后,您将使用AWS S3验证的URLS来服务。



正确的URL将如下所示:

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

{%static%} 模板标签可以添加签名。使用 STATIC_URL 不会。



指纹网址



以类似的方式,如果您的静态文件存储指纹您的文件,使用 STATIC_URL 将无法正常工作。


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.

解决方案

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.

这篇关于使用{{STATIC_URL}}与{%static%}之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆