Django STATIC_ROOT、STATIC_URL和STATICFILES_DIRS

settings.py

# static settings
# BASE_DIR 项目的绝对路径
STATIC_URL = '/static/'
STATICFILES_DIRS=[BASE_DIR / 'static',]
# STATIC_ROOT = os.path.join(BASE_DIR, 'collect_static') 需要 import os
STATIC_ROOT = BASE_DIR / 'collect_static'

1、STATIC_ROOT

STATIC_ROOT 在部署静态文件时,所有的静态文静聚合的目录,需要绝对路径。

在运行 Nginx 之前,必须在静态文件夹中搜集所有的 Django 静态文件。首先你需要编辑 settings.py 添加:

STATIC_ROOT = BASE_DIR / 'collect_static'

然后执行:

python manage.py collectstatic

django 会把所有的 static 文件都复制到 STATIC_ROOT 文件夹下

2、STATICFILES_DIRS

Django 项目 存放静态文件
1)在每个 app 中创建 static 文件夹
2)在项目跟目录下创建 static 文件夹

配置全局静态文件夹:
STATICFILES_DIRS=[BASE_DIR / ‘static’,]

静态文件查找顺序:首先 STATICFILES_DIRS,然后各个 app 中 static 文件夹,是惰性查找。

app 可以跨域应用静态文件,最后所有的静态文件都会聚合在STATIC_ROOT中。

3、STATIC_URL

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

https://docs.djangoproject.com/en/1.11/ref/settings/#std:setting-STATIC_ROOT

Static Files¶
 Settings for django.contrib.staticfiles.STATIC_ROOT¶
 Default: NoneThe absolute path to the directory where collectstatic will collect static files for deployment.
Example: “/var/www/example.com/static/”
If the staticfiles contrib app is enabled (as in the default project template), the collectstatic management command will collect static files into this directory. See the how-to on managing static files for more details about usage.
Warning
This should be an initially empty destination directory for collecting your static files from their permanent locations into one directory for ease of deployment; it is not a place to store your static files permanently. You should do that in directories that will be found by staticfiles’s finders, which by default, are ‘static/’ app sub-directories and any directories you include in STATICFILES_DIRS).
STATIC_URL¶
 Default: NoneURL to use when referring to static files located in STATIC_ROOT.
Example: “/static/” or “http://static.example.com/”
If not None, this will be used as the base path for asset definitions (the Media class) and the staticfiles app.
It must end in a slash if set to a non-empty value.
You may need to configure these files to be served in development and will definitely need to do so in production.
STATICFILES_DIRS¶
 Default: [] (Empty list)This setting defines the additional locations the staticfiles app will traverse if the FileSystemFinder finder is enabled, e.g. if you use the collectstatic or findstatic management command or use the static file serving view.
This should be set to a list of strings that contain full paths to your additional files directory(ies) e.g.:
STATICFILES_DIRS = [
 “/home/special.polls.com/polls/static”,
 “/home/polls.com/polls/static”,
 “/opt/webfiles/common”,
 ]
 Note that these paths should use Unix-style forward slashes, even on Windows (e.g. “C:/Users/user/mysite/extra_static_content”).Prefixes (optional)¶
 In case you want to refer to files in one of the locations with an additional namespace, you can optionally provide a prefix as (prefix, path) tuples, e.g.:STATICFILES_DIRS = [
 # …
 (“downloads”, “/opt/webfiles/stats”),
 ]
 For example, assuming you have STATIC_URL set to ‘/static/’, the collectstatic management command would collect the “stats” files in a ‘downloads’ subdirectory of STATIC_ROOT.This would allow you to refer to the local file ‘/opt/webfiles/stats/polls_20101022.tar.gz’ with ‘/static/downloads/polls_20101022.tar.gz’ in your templates, e.g.:
<a href="{% static “downloads/polls_20101022.tar.gz” %}">