Django服务非app内静态文件

有一些静态文件是共用的,比如bower_components,但是其自身不是一个app,Django提供了一个设置STATICFILES_DIRS是专门服务这类静态文件的。

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.

假设在项目根目录下安装bower_components

>bower install bootstrap

设置

STATIC_URL = '/static/'
STATIC_ROOT = 'assets'

STATICFILES_DIRS = [
     os.path.join(BASE_DIR, "bower_components"),
]

settings.py设置中STATIC_ROOT表示待静态文件存放的目录,STATICFILES_DIRS的类型是list,表示哪些目录里面包含了待收集的静态文件。

例如项目navmock中有如下目录和文件

$ ls
assets/  bower_components/  db.sqlite3  manage.py*  navmock/

运行collectstatic命令

>python manage.py collectstatic --noinput

那么assets文件夹中现在包含有app的static文件和bower_components里面的文件夹,例如jquery, bootstrap等。

$ ls assets/
admin/  bootstrap/ jquery/

runserver

>python manage.py runserver --insecure

参考:https://docs.djangoproject.com/en/1.9/howto/static-files/

关于本文如您有任何想法和意见,欢迎与我们联系,邮箱地址zhi@uqugu.com
您对本文有什么看法,喜欢或者不喜欢都可以发表意见。