我在一个项目文件夹中同时拥有一个Django和React应用程序。
项目结构:
project/
frontend/
src/
components/
App.jsx
index.js
static/
frondend/
main.js
templates/
frondend/
index.html
migrations/
__init__.py
admin.py
apps.py
models.py
test.py
urls.py
views.py
project/
migrations/
__init__.py
admin.py
apps.py
models.py
test.py
urls.py
views.py
nodemodules/
.bablerc
manage.py
.webpack.config.js
package-lock.json
package.json
index.html:
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bulma/0.6.2/css/bulma.min.css">
<title>Django DRF - React : Quickstart - </title>
</head>
<body>
<section>
<div>
<div id="root">
<!-- React -->
</div>
</div>
</section>
</body>
{% load static %}
<script src="{% static 'frontend/main.js' %}"></script>
</html>
我的.bablerc文件: {
"presets": [
"es2015", "stage-1", "react"
], “插件”:[ “转换运行时”, “转换装饰器的遗产”, “转换类属性”, “转换对象休息传播”, “反应热式装载机/风琴”, “类型检查”, “ transform-es2015-modules-commonjs”, “添加模块出口”, “导入声明” ] }
我的webpack.config.js:
const HtmlWebPackPlugin = require("html-webpack-plugin");
module.exports = {
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
loader: "babel-loader",
query: { presets: ["@babel/preset-env", "@babel/preset-react"] }
},
{
test: /\.html$/,
use: [
{
loader: "html-loader",
options: { minimize: true }
}
]
}
]
},
plugins: [
new HtmlWebPackPlugin({
template: "./frontend/templates/frontend/index.html",
filename: "./index.html"
})
]
};
这里的问题是我的静态文件不会被加载
{% load static %}
声明。
dev : 从project.settings.com通用导入*
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = ['127.0.0.1', 'localhost']
DATABASE_ROUTERS = ['stockdata.routers.StockdataRouter',]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dbname',
'USER': 'postgres',
'PASSWORD': '*****',
'HOST': 'localhost',
'PORT': '5432',
'OPTIONS': {
'connect_timeout': 0,
}
},
'data_db': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'sdata',
'USER': 'postgres',
'PASSWORD': '*****',
'HOST': 'localhost',
'PORT': '5432',
},
}
STATIC_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, "static"),
os.path.join(BASE_DIR, "frontend/static/"),
]
生产设置:
from project.settings.common import *
DEBUG = False
TEMPLATE_DEBUG = True
ALLOWED_HOSTS = ['aws.ip',]
DATABASE_ROUTERS = ['stockdata.routers.StockdataRouter',]
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'dbname',
'USER': 'awsuser',
'PASSWORD': '******',
'HOST': '*****.******.ap-south-1.rds.amazonaws.com',
'PORT': '5432',
# 'HAS_HSTORE': True,
'OPTIONS': {
'connect_timeout': 0,
},
'CONN_MAX_AGE': 0
},
'data_db': {
'ENGINE': 'django.db.backends.postgresql_psycopg2',
'NAME': 'data_db',
'USER': 'awsuser',
'PASSWORD': '*******',
'HOST': '*****.******.ap-south-1.rds.amazonaws.com',
'PORT': '5432',
'CONN_MAX_AGE': 0
}
}
STATIC_ROOT = os.path.join(BASE_DIR, "static")
STATIC_URL = '/static/'
STATICFILES_FINDERS = [
'django.contrib.staticfiles.finders.FileSystemFinder',
'django.contrib.staticfiles.finders.AppDirectoriesFinder',
]
有没有办法可以在React HTML中将我的静态文件加载到这里。
而且,如果这种配置方式似乎是错误的方法,那么请帮助我逐步实现Django + React项目。
谢谢。
搜索后,我发现问题出在与“ Base_Url”有关,因为如果点击“ 127.0.0.1:8000/static/frontend/main.js”,这将成功打开我的js文件。所以有人可以帮我解决这个问题。