【问题标题】:Django frontend app not loading React ComponentDjango 前端应用程序未加载 React 组件
【发布时间】:2020-05-22 08:16:55
【问题描述】:

我关注tranversy Media Django-React tutorial。服务器运行ok后,但是我的App.js文件中的组件没有显示在index.html中。请大家帮忙看看可能出了什么问题。

这是我的项目树: see the image

然后是我的 app.js

import React, {Component} from 'react';
import ReactDOM from 'react-dom';

class App extends Component {
    render() {
        return <h1>React App</h1>
    }
}
ReactDOM.render(<App />, document.getElementById("app"));

我的 index.html

<meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://bootswatch.com/4/cosmo/bootstrap.min.css">  

    <title>Lead Manager</title>

    {%load static%}

</head>

<body>
    <h1></h1>
    <div id="app"></div>

    <script>src="{%static "frontend/main.js"%}"</script>
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>

index.js

import App from './components/App';

webpack.config.js

module.exports = {
    module: {
        rules: [{
            test: /\.js$/,
            exclude: /node_modules/,
            use: {
                loader:"babel-loader"
            }
        }]
    }
}

后端使用 api 工作正常。

【问题讨论】:

    标签: django reactjs django-rest-framework


    【解决方案1】:

    改变这个

    <script>src="{%static "frontend/main.js"%}"</script>
    

    到这里

    <script src="{%static "frontend/main.js"%}"></script>
    

    【讨论】:

    • 非常感谢,不敢相信这是一个愚蠢的错误
    猜你喜欢
    • 2017-08-21
    • 2022-11-08
    • 2019-07-18
    • 2017-03-26
    • 1970-01-01
    • 1970-01-01
    • 2017-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多