我对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).
更多推荐
发布评论