Django中STATIC

编程入门 行业动态 更新时间:2024-10-18 06:02:28
Django中STATIC_URL和STATIC_ROOT有什么区别?(What's the difference between STATIC_URL and STATIC_ROOT in Django?)

我对Django的'staticfiles'应用程序中的STATIC_URL和STATIC_ROOT之间的区别有些困惑。

我相信我明白什么是STATIC_ROOT :它基本上是服务器上的位置,staticfiles的collectstatic命令将放置从您的django项目收集的静态文件。 collectstatic命令将搜索您在STATIC_FINDERS设置中指定的位置。

但是, STATIC_URL干什么? 这应该设置为什么? 显然它的目的是为了让用户可以访问静态文件。 但是它与STATIC_ROOT什么关系?

为什么STATIC_URL的默认值只是/static/ ? STATIC_URL是否必须能够引用STATIC_ROOT ?

I'm somewhat confused as to what the difference is between STATIC_URL and STATIC_ROOT in Django's 'staticfiles' app.

I believe I understand what the STATIC_ROOT is: it's essentially the location on the server where the staticfiles' collectstatic command will place the static files collected from your django project. The collectstatic command searches in the locations that you specify in the STATIC_FINDERS setting.

However, what exactly does the STATIC_URL do? What should this be set to? Apparently it's intended to be set something such that users can access static files. But what is it's relationship with STATIC_ROOT?

Why is the default value of STATIC_URL simply /static/ ? Does STATIC_URL have to be able to reference STATIC_ROOT?

最满意答案

就像你提到的那样,从文档中可以清楚地看到:

STATIC_ROOT:

collectstatic将收集用于部署的静态文件的目录的绝对路径。

STATIC_URL

默认值:无

引用位于STATIC_ROOT静态文件时使用的URL。

例如: "/static/"或"http://static.example.com/"

虽然STATIC_ROOT只是收集静态文件的目录的路径,但STATIC_URL是将为这些静态文件提供服务的URL。

而且,正如您在示例中所看到的那样,您可以将STATIC_URL定义为子域"http://static.example.com/"并且在模板中使用它时:

<link rel="stylesheet" href="{{ STATIC_URL }}css/base.css" type="text/css" />

它将被视为:

<link rel="stylesheet" href="http://static.example.com/css/base.css" type="text/css" />

但是,如果STATIC_URL只是/static/那么上面的链接将指向:

<link rel="stylesheet" href="/static/css/base.css" type="text/css" />

而且,由于这个href以/开始,它会追加你的域来访问静态文件: http://yourdomain/static/css/base/css


为什么STATIC_URL的默认值只是/static/ ? STATIC_URL是否必须能够引用STATIC_ROOT ?

您可以在文档中看到STATIC_URL默认值不是/static/但是无。 而且,它不必引用STATIC_ROOT因为它不依赖于它(如上例所示)。

Like you mentioned, it is pretty clear from the documentation:

STATIC_ROOT:

The absolute path to the directory where collectstatic will collect static files for deployment.

STATIC_URL

default: None

URL to use when referring to static files located in STATIC_ROOT.

Example: "/static/" or "http://static.example.com/"

While, the STATIC_ROOT is just the path to the directory where static files have been collected, STATIC_URL is the URL which will serve those static files.

And, as you can see in the example, you can define STATIC_URL as a subdomain "http://static.example.com/" and when you use it in the template:

<link rel="stylesheet" href="{{ STATIC_URL }}css/base.css" type="text/css" />

It will be treated as:

<link rel="stylesheet" href="http://static.example.com/css/base.css" type="text/css" />

But, if the STATIC_URL was just /static/ then the above link would point to:

<link rel="stylesheet" href="/static/css/base.css" type="text/css" />

And, since this href starts with / it will append your domain to access the static files: http://yourdomain/static/css/base/css


Why is the default value of STATIC_URL simply /static/ ? Does STATIC_URL have to be able to reference STATIC_ROOT?

Default value of STATIC_URL is not /static/ but None as you can see in the documentation. And, it doesn't have to reference to STATIC_ROOT because it is not dependent on it (as shown in the example above).

更多推荐

本文发布于:2023-04-28 06:43:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1330345.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Django   STATIC

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!