项目结构:
├── 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:
<head>
{% load staticfiles %}
<link href="{% static 'css/bootstrap.css' %}" rel="stylesheet">
<link href="{% static 'css/theme.css' %}" rel="stylesheet">
<link href="{% static 'css/magnific-popup.css' %}" rel="stylesheet">
</head>
<body>
<script src="js/bootstrap.min.js"></script>
<script src="js/cbpAnimatedHeader.min.js"></script>
<script src="js/owl.carousel.min.js"></script>
</body>
错误日志:
[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文件。 有人可以指出我的错误吗? (无法弄清楚确切的问题/原因,所以不得不提到很多细节)
谢谢,
答案 0 :(得分:5)
在项目的主文件夹中创建一个目录static
,其中包含manage.py
并将所有静态文件放在此文件夹中。为css
,js
,img
创建文件夹,并像以前一样加载它:
<link rel="stylesheet" link href="{% static 'css/bootstrap.min.css' %}"/>
使用此设置:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATICFILES_DIRS = (os.path.join(BASE_DIR, 'static'),)
在路径../static