项目结构:
??? db.sqlite3 ??? manage.py ??? static ? ??? admin ? ??? css ? ??? img ? ??? js ??? abc1 ? ??? admin.py ? ??? __init__.py ? ??? migrations ? ? ??? __init__.py ? ??? models.py ? ??? templates ? ? ??? abc1 ? ? ??? homepage.html ? ? ??? css ? ? ??? img ? ? ??? js ? ??? tests.py ? ??? urls.py ? ??? views.py ??? abc2 ??? functions.py ??? __init__.py ??? settings.py ??? urls.py ??? views.py ??? wsgi.py
Settings.py文件:
MEDIA_URL = '/media/' STATIC_URL = '/static/' PROJECT_DIR = os.path.dirname(__file__) SETTINGS_PATH = os.path.dirname(os.path.dirname(__file__)) # Static files (CSS, JavaScript, Images) STATIC_ROOT = '' STATICFILES_DIRS = ( os.path.join(PROJECT_DIR, '../static'), os.path.join(PROJECT_DIR, '../templates/abc1/img'), )
homepage.html:
{% load staticfiles %}
错误日志:
[08/Jan/2016 17:16:42] "GET /static/css/theme.css HTTP/1.1" 404 1643 [08/Jan/2016 17:16:42] "GET /static/css/bootstrap.css HTTP/1.1" 404 1655 [08/Jan/2016 17:16:42] "GET /static/css/magnific-popup.css HTTP/1.1" 404 1670 [08/Jan/2016 17:16:42] "GET /abc1/js/owl.carousel.min.js HTTP/1.1" 404 3579 [08/Jan/2016 17:16:42] "GET /abc1/js/bootstrap.min.js HTTP/1.1" 404 3570 [08/Jan/2016 17:16:42] "GET /abc1/js/cbpAnimatedHeader.min.js HTTP/1.1" 404 3594
我试图在Django中包含静态css/js/img文件.有人可以指出我的错误吗?(无法弄清楚确切的问题/原因,所以不得不提到很多细节)
谢谢,
static
在项目的主文件夹中创建一个目录,其中包含manage.py
并将所有静态放在此文件夹中.为创建文件夹css
,js
,img
,和像你这样之前加载它:
使用此设置:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) STATIC_URL = '/static/' STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
而你在路径之前不需要这些点 ../static