Django中的STATIC_URL和STATIC_ROOT有什么区别? [英] What's the difference between STATIC_URL and STATIC_ROOT in Django?
问题描述
对于Django的'staticfiles'
应用程序中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.
我相信我了解 STATIC_ROOT
是什么:本质上,这是服务器上静态文件的 collectstatic
的位置。命令将放置从您的django项目收集的静态文件。 collectstatic
命令在 STATIC_FINDERS
设置中指定的位置搜索。
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.
但是, STATIC_URL
到底是做什么的?应该设置为什么?显然,它旨在进行一些设置,以便用户可以访问静态文件。但是与 STATIC_ROOT
有什么关系?
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
?
为什么默认值 STATIC_URL
只需 / static /
? STATIC_URL
是否必须能够引用 STATIC_ROOT
?
Why is the default value of STATIC_URL
simply /static/
? Does STATIC_URL
have to be able to reference STATIC_ROOT
?
推荐答案
就像您提到的,从文档中可以很清楚地看到:
Like you mentioned, it is pretty clear from the documentation:
STATIC_ROOT:
collectstatic
将收集用于部署的静态文件的目录的绝对路径。
The absolute path to the directory where collectstatic
will collect static files for deployment.
STATIC_URL
默认:无
引用位于 STATIC_ROOT中的静态文件时使用的URL
。
示例: / static /
或 http://static.example.com/
而 STATIC_ROOT
只是收集静态文件的目录的路径, STATIC_URL
是将为这些静态文件提供服务的URL。
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.
并且,如您在示例中看到的,可以将 STATIC_URL
定义为子域 http://static.example。 com /
,并在模板中使用它时:
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" />
它将被视为:
<link rel="stylesheet" href="http://static.example.com/css/base.css" type="text/css" />
但是,如果 STATIC_URL
只是 / static /
,则上面的链接将指向:
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" />
而且,由于此 href
以 /
它将附加您的域以访问静态文件: http:// yourdomain / static / css / base / css
And, since this href
starts with /
it will append your domain to access the static files: http://yourdomain/static/css/base/css
为什么默认值
STATIC_URL
只是/ static /
? STATIC_URL是否必须能够引用STATIC_ROOT
?
Why is the default value of
STATIC_URL
simply/static/
? Does STATIC_URL have to be able to referenceSTATIC_ROOT
?
默认值 STATIC_URL
不是 / static /
,而是无,正如您在文档中看到的那样。而且,它不必引用 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).
这篇关于Django中的STATIC_URL和STATIC_ROOT有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!