django静态文件的配置

时间:2023-02-01 20:18:29

参考一下的博客就行,我的是1.10版本的:

http://yeelone.blog.51cto.com/1476571/863583

 

另外还有可以加深理解的:

http://www.tuicool.com/articles/fyaqUrV

http://www.cnblogs.com/Rainday/p/4195111.html

 

官方文档的描述:

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" %}">

再看我自己的配置:
STATIC_URL = '/static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'static').replace('\\','/')

STATICFILES_DIRS = (
os.path.join(BASE_DIR, "blog/static").replace('\\','/'),
)

所以意思就是,

'/static/' 替换了
os.path.join(BASE_DIR, "blog/static") 这个前缀
所以在模板中引入文件就有
<link rel="stylesheet" href="/static/base/style.css">

其中项目结构如下:
django静态文件的配置

 

***************************分界线(更新于2017/1/13)***********************

STATICFILES_FINDERS = (
"django.contrib.staticfiles.finders.FileSystemFinder",
"django.contrib.staticfiles.finders.AppDirectoriesFinder"
)

这个和上面的几行代码同时写在settings.py文件中,是系统用来查找静态文件的。

1. STATIC_ROOT 文件夹是用来收集所有的静态文件的,方便后面我们用 nginx, apache 等来部署
2. STATICFILES_DIRS 是用来放一些不属于app,共用的文件,比如 jquery.min.js 这种类型的

如果定义了STATICFILES_DIRS参数,则默认先查找这个参数定义的文件(usingdjango.contrib.staticfiles.finders.FileSystemFinder),找到后则停止,不会再继续查找各个APP下的static文件夹(usingdjango.contrib.staticfiles.finders.AppDirectoriesFinder)。