首先,django的有两个静态文件查找器:
("django.contrib.staticfiles.finders.FileSystemFinder", "django.contrib.staticfiles.finders.AppDirectoriesFinder")
FileSystemFinder是查找setting文件 STATICFILES_DIRS 参数配置下的目录, 而AppDirectoriesFinder顾名思义,是查找project下面,每个app目录下的static目录下的文件。
假设一个project的目录如下:
testpro
-- testapp1
-- static
my.js
__init__.py
tests.py
models.py
views.py
--templates
test.html
__init__.py
manage.py
settings.py
urls.py
--static
--js
global.js
要在test.html里面引用my.js,如下:
<script src="/static/my.js"></script>
要引用global.js的话,要首先在settings.py里面设置如下:
STATICFILES_DIRS = (
"E:/MyProjects/django/todolist/static",
)
在test.html里引用(类似template的调用):
<script src="/static/global.js"></script>
要特别说明的是,上面的设置专门用于development环境,这些静态文件都设在local。如果是在production环境,static files在另外一台server上,就要另外设置,在这里先不做讨论。