Django 上的静态 STATIC_URL 和 STATIC_ROOT 之间的区别 [英] Difference between static STATIC_URL and STATIC_ROOT on Django
问题描述
我对 static root
感到困惑,想澄清一些事情.
要在 Django 中提供静态文件,settings.py
和 urls.py
中应包含以下内容:
导入操作系统PROJECT_DIR=os.path.dirname(__file__)
1.应该收集静态文件的目录的绝对路径
STATIC_ROOT= os.path.join(PROJECT_DIR,'static_media/')
2.静态文件的 URL 前缀
STATIC_URL = '/static/'
3.静态文件的其他位置
STATICFILES_DIRS = ( os.path.join(PROJECT_DIR,'static/'),)
...并在 urls.py
中添加以下几行:
from django.contrib.staticfiles.urls import staticfiles_urlpatternsurlpatterns += patterns('', (r'^static/(?P.*)$','django.views.static.serve',{'document_root': settings.STATIC_ROOT}))
4.我们也使用 python manage.py collectstatic
问题:
谁能向我解释一下工作流程:理想情况下应该如何完成工作.到目前为止,我将上面的代码片段复制/粘贴到指定位置并继续在静态目录中创建新文件并且它可以工作.然而,在我的
settings.STATIC_ROOT
中,我指向了一个不同的目录.如果有人能够解释每个设置的工作流程,那就太好了:如何收集和管理文件,以及遵循哪些好的做法.
谢谢.
STATIC_ROOT
<块引用>./manage.py collectstatic
将收集用于部署的静态文件的目录的绝对路径.示例:STATIC_ROOT="/var/www/example.com/static/"
现在命令 ./manage.py collectstatic
会将所有静态文件(即在您的应用程序中的静态文件夹中,所有路径中的静态文件)复制到目录 /var/www/example.com/static/
.现在你只需要在 apache 或 nginx..etc 上提供这个目录.
STATIC_URL
<块引用>STATIC_ROOT
目录下的静态文件所服务的 URL
(通过 Apache 或 nginx..etc).示例:/static/
或 http://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' with 'http://static.example.com/jquery.js'
)
现在在您的 django 模板中,您可以通过以下方式引用它:
{% 加载静态 %}<script src="{% static "jquery.js" %}"></script>
将呈现:
<script src="http://static.example.com/jquery.js"></script>
I am confused by static root
and want to clarify things.
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. Absolute path to the directory in which static files should be collected
STATIC_ROOT= os.path.join(PROJECT_DIR,'static_media/')
2. URL prefix for static files
STATIC_URL = '/static/'
3. Additional locations for static files
STATICFILES_DIRS = ( os.path.join(PROJECT_DIR,'static/'),)
...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. We also use python manage.py collectstatic
Questions:
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.
Thanks.
STATIC_ROOT
The absolute path to the directory where
./manage.py collectstatic
will collect static files for deployment. Example:STATIC_ROOT="/var/www/example.com/static/"
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.
STATIC_URL
The
URL
of which the static files inSTATIC_ROOT
directory are served(by Apache or nginx..etc). Example:/static/
orhttp://static.example.com/
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'
)
Now in your django-templates, you can refer it by:
{% load static %}
<script src="{% static "jquery.js" %}"></script>
which will render:
<script src="http://static.example.com/jquery.js"></script>
这篇关于Django 上的静态 STATIC_URL 和 STATIC_ROOT 之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!