我的Django版本是1.9
下图是我的目录结构:
dashboard是我的app名
我在settings.py中加入了这2句
STATIC_URL = '/static/' STATIC_ROOT = os.path.join(BASE_DIR, 'static')
在html中我是这样写的
{% load staticfiles %}
最后运行测试服务器打开网页,出来下图的错误TAT。。
下图是后台的日志
我在网上找了各种办法,都不行,各路大神们这是为什么!!!!
搞定了。。。
在settings.py中:
STATIC_URL = 'static/' STATIC_ROOT = os.path.join(BASE_DIR, 'dashboard/static')
在dashboard/urls.py中:
# coding:utf8 from django.conf.urls import url from django.conf.urls.static import static from . import views from django.conf import settings app_name = 'dashboard' urlpatterns = [ url(r'^$', views.index, name='index'), ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
搞定!
templates
,static
等放在app目录下即可自动找到,不会出现找不到情况。当然这是在debug=True
的情况下,
当debug=False情况下,使用python manager.py collectstatic
命令,就会自动把所有引用的静态文件和模板等复制到STATIC_ROOT
文件夹下面。也解决了。
settings.py中添加
STATICFILES_DIRS = ( os.path.join(BASE_DIR, 'static/'), '/statics/js/', '/statics/css/', )
我记得好像template可以跟随app放置,但是static还是需要统一collect一下才能用。
python manage.py collectstatic
https://docs.djangoproject.com/en/1.9/ref/contrib/staticfiles/#django-admin-collectstatic