【发布时间】:2017-09-13 11:18:28
【问题描述】:
我正在尝试包含不在乐队应用程序中的静态文件,而是在名为 static 的单独目录中。
此目录包含一个 build 目录,其中包含 CSS 和 JS 目录。
除了 CSS 和 JS 目录中的文件之外,我还想包含 bower_components 目录中的缩小文件,例如 jquery、react 和 browser。
我的项目结构如下:
Music/
├── band/
│ ├── migrations/
│ ├── __init__.py
│ ├── admin.py
│ ├── apps.py
│ ├── models.py
│ ├── serializers.py
│ ├── tests.py
│ ├── urls.py
│ └── views.py
├── music/
│ ├── __init__.py
│ ├── settings.py
│ ├── urls.py
│ ├── views.py
│ └── wsgi.py
├── node_modules/
├── static/
├──── static/
│ ├── bower_components/
│ ├── build/
│ ├── css/
│ ├── images/
│ └── js/
├── templates/
├── band/
├── MasterPages/
├── index.html
我在 music 中的 settings.py 包含以下内容:
STATIC_URL = '/static/'
STATIC_ROOT = 'C:/Users/Broski/PycharmProjects/Music/static'
MEDIA_ROOT = 'C:/Users/Broski/PycharmProjects/Music/upload'
此 MasterPage.html 文件位于 MasterPages 目录的模板下:
{% load staticfiles %}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<title>Music</title>
<link rel="stylesheet" href="{% static 'build/css/main.css' %}" type="text/css" />
</head>
<body>
{% block content %}
{% endblock %}
<script src="{% static 'bower_components/jquery/dist/jquery.min.js' %}"></script>
<script src="{% static 'bower_components/react/react.min.js' %}"></script>
<script src="{% static 'bower_components/babel/browser.min.js' %}"></script>
<script type="text/babel" src="{% static 'build/js/indexReact.js' %}"></script>
</body>
</html>
当我加载网站时,模板会呈现,但静态文件不会。我收到 404 错误。
GET http://127.0.0.1:8000/static/css/main.css
127.0.0.1/:22 GET http://127.0.0.1:8000/static/bower_components/jquery/dist/jquery.min.js
127.0.0.1/:23 GET http://127.0.0.1:8000/static/bower_components/react/react.min.js
127.0.0.1/:26 GET http://127.0.0.1:8000/static/bower_components/babel/browser.min.js 404 (Not Found)
如您所见,我需要 React、Jquery 和 Browser 正确加载才能继续这个 youtube 教程:
Building a website with Django and React.js Video 1: Defining our project structure
谢谢
【问题讨论】: