javascript - DJango React 未找到
问题描述
我是 DJango 和 React 的新手。我在 Pycharm 中创建了一个 DJango 项目,然后使用npm create-react-app frontend
. 我在文件夹“src”中添加了一些 JavaScript 文件并编辑了一个自动创建的 index.css。我构建了 React 应用程序,然后启动了 DJango 服务器。控制台打印:
Not Found: /src/index.css
Not Found: /src/seigaiha.js
[25/Dec/2020 14:23:31] "GET /src/index.css HTTP/1.1" 404 2201
[25/Dec/2020 14:23:31] "GET /src/seigaiha.js HTTP/1.1" 404 2207
但是 .css 文件在已启动的站点上是可见的。.js 不是。
在文件夹中订购:
- frontend/
- - build/
- - - static/
- - - - css/ #here .css file after build has the same structure as index.css, so it buil
- - - - js/
- - - - media/
- - node-modules/
- - public/
- - src/
- - - index.css
- - - seigaiha.js
在 settings.py 中:
'DIRS': [
os.path.join(BASE_DIR / 'frontend/build')
]
在public/index.html
:
***some code***
<link rel="stylesheet" type="text/css" href="./src/index.css">
***some code***
<div class="pattern">
<canvas id="canvas">Canvas not supported.</canvas>
<script src="src/seigaiha.js"></script>
</div>
***some code***
我做错了什么?也许我需要将这些文件放在另一个文件夹中?或者可能路径是错误的?.js 文件名?当我只有 .html、.css 和 js 文件(没有反应)时,一切正常。
UPD:我在 stackoverflow 中阅读了一些建议,并使用npm start
了 instesd 的命令npm run build
。它没有用。
解决方案
尝试这样的事情:
FRONTEND_DIR = os.path.abspath(os.path.join(BASE_DIR, "..", "front-end"))
TEMPLATES = [
{
"BACKEND": "django.template.backends.django.DjangoTemplates",
"DIRS": [os.path.join(FRONTEND_DIR, "build")],
"APP_DIRS": True,
"OPTIONS": {
"context_processors": [
"django.template.context_processors.debug",
"django.template.context_processors.request",
"django.contrib.auth.context_processors.auth",
"django.contrib.messages.context_processors.messages",
],
},
},
]
STATICFILES_DIRS = (os.path.join(FRONTEND_DIR, "build", "static"),)
推荐阅读
- javascript - 通过 Shadow DOM 使用composed=false 传递标准JavaScript 事件
- eclipse - Maven2 安装:缺少工件
- bluetooth - “启用角色切换”(来自链接策略设置)和“允许角色切换”(在 ACL 连接中)之间的区别?蓝牙 BR/EDR/LE
- javascript - 我想根据一个属性对一组对象进行排序。将值推送到具有与其排序相同的属性的第二个属性中
- homebrew - Homebrew - 请求的 URL 已重新调整错误:404
- pytorch - 向量化代码并从 pytorch 代码中删除嵌套循环
- postgresql - count(*) 返回少于实际行数
- python - 如何使用带有 appId 和 processId 异常的 appium 和 python 定位打开的应用程序窗口?
- sql - 在事件发生后的当天找到每种事件类型的平均值
- python - PushShift:从时间范围内抓取提交