Django上静态STATIC_URL和STATIC_ROOT之间的区别 [英] Difference between static STATIC_URL and STATIC_ROOT on Django
问题描述
我对静态根
感到困惑,并想澄清一些事情。
I am confused by static root
and want to clarify things.
要在Django中提供静态文件,以下应位于 settings.py
和 urls.py
中:
To serve static files in Django, the following should be in settings.py
and urls.py
:
import os
PROJECT_DIR=os.path.dirname(__file__)
1。静态文件应该收集到的目录的绝对路径
1. Absolute path to the directory in which static files should be collected
STATIC_ROOT= os.path.join(PROJECT_DIR,'static_media/')
2。静态文件的URL前缀
2. URL prefix for static files
STATIC_URL = '/static/'
3。静态文件的其他位置
3. Additional locations for static files
STATICFILES_DIRS = ( os.path.join(PROJECT_DIR,'static/'),)
...以及 urls.py
中的以下行:
...and in urls.py
the following lines:
from django.contrib.staticfiles.urls import staticfiles_urlpatterns
urlpatterns += patterns('', (
r'^static/(?P<path>.*)$',
'django.views.static.serve',
{'document_root': settings.STATIC_ROOT}
))
4。我们还使用 python manage.py collectstatic
问题:
4. We also use python manage.py collectstatic
Questions:
-
有人可以向我解释工作流程:理想情况下应该如何做。到目前为止,我将上述代码段复制/粘贴到它们的指定位置,并继续在静态目录中创建新文件,并且可以正常工作。但是,在我的
settings.STATIC_ROOT
中,我指向了另一个目录。
Could anyone please explain the workflow to me: how should things ideally be done. As of now, I copy/paste the above code snippets into their designated locations and continue making new files in the static directory and it works. In my
settings.STATIC_ROOT
, however, I have pointed to a different directory.
如果有人可以解释每种设置的工作流程,那就太好了:文件的收集和管理方式以及遵循的良好做法。
It would be great if someone could explain the workflow of each setting: how files are collected and managed, and what would be a good practice to follow.
谢谢。
推荐答案
STATIC_ROOT
STATIC_ROOT
绝对
./ manage.py collectstatic
将收集用于部署的静态文件的目录的路径。
示例:STATIC_ROOT = / var / www / example.com / static /
The absolute path to the directory where
./manage.py collectstatic
will collect static files for deployment. Example:STATIC_ROOT="/var/www/example.com/static/"
现在命令。/manage.py collectstatic
将复制所有静态文件(即应用程序中的静态文件夹) (所有路径中的静态文件)到目录 /var/www/example.com/static /
。现在您只需要在apache或nginx..etc上提供此目录。
now the command ./manage.py collectstatic
will copy all the static files(ie in static folder in your apps, static files in all paths) to the directory /var/www/example.com/static/
. now you only need to serve this directory on apache or nginx..etc.
URL
,其中STATIC_ROOT
目录中的静态文件由Apache或nginx..etc提供。
示例:/ static /
或http://static.example.com/
The
URL
of which the static files inSTATIC_ROOT
directory are served(by Apache or nginx..etc). Example:/static/
orhttp://static.example.com/
如果您设置 STATIC_URL ='http://static.example.com/'
,则必须提供 STATIC_ROOT
文件夹(即 / var / www / example.com / static /
),由apache或nginx在url 'http://static.example.com/'
(这样您可以引用静态文件'/ var / www / example.com / static / jquery.js'
和'http://static.example.com/jquery.js'
)
If you set STATIC_URL = 'http://static.example.com/'
, then you must serve the STATIC_ROOT
folder (ie "/var/www/example.com/static/"
) by apache or nginx at url 'http://static.example.com/'
(so that you can refer the static file '/var/www/example.com/static/jquery.js'
with 'http://static.example.com/jquery.js'
)
现在在Django模板中,可以通过以下方式引用它:
Now in your django-templates, you can refer it by:
{% load static %}
<script src="{% static "jquery.js" %}"></script>
它将呈现:
<script src="http://static.example.com/jquery.js"></script>
这篇关于Django上静态STATIC_URL和STATIC_ROOT之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!