Django + React在同一开发服务器/端口上

时间:2019-05-03 22:44:26

标签: javascript python node.js django reactjs

我正在尝试在Django运行的同一服务器和端口(django受管服务器)上启动我的React。似乎已正确设置了ULR,因为我可以加载/public/index.html React页面,但无法加载我的应用程序,只能加载静态HTML。当我启动django服务器时,它会出现:

python manage.py runserver

当我从以下位置启动React应用程序时:     npm start(节点), 它工作正常,我可以启动Django服务器(不同的端口,相同的本地主机);他们那样一起工作,但这不是我想要的。

浏览代码,我发现如果我使用Node服务器启动React应用,则会自动添加脚本行(请参见最后一个标记):

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <meta name="theme-color" content="#000000" />
    <!--
      manifest.json provides metadata used when your web app is installed on a
      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
    -->
    <link rel="manifest" href="/manifest.json" />
    <!--
      Notice the use of  in the tags above.
      It will be replaced with the URL of the `public` folder during the build.
      Only files inside the `public` folder can be referenced from the HTML.

      Unlike "/favicon.ico" or "favicon.ico", "/favicon.ico" will
      work correctly both with client-side routing and a non-root public URL.
      Learn how to configure a non-root public URL by running `npm run build`.
    -->
    <script src="../src/index.js"></script>
    <title>Aplicación </title>
  </head>
  <body>
    <noscript>You need to enable JavaScript to run this app.</noscript>
    <div id="root"></div>
    <!--
      This HTML file is a template.
      If you open it directly in the browser, you will see an empty page.

      You can add webfonts, meta tags, or analytics to this file.
      The build step will place the bundled scripts into the <body> tag.

      To begin the development, run `npm start` or `yarn start`.
      To create a production bundle, use `npm run build` or `yarn build`.
    -->
  <script src="/static/js/bundle.js"></script><script src="/static/js/1.chunk.js"></script><script src="/static/js/main.chunk.js"></script></body>
</html>

从ulrs.py中设置的Django URL调用React应用时,此行不会出现

我尝试过: -复制并粘贴index.html文件中的“脚本”行,以加载与节点服务器相同的内容,但是结果是相同的。 -从另一个“脚本”标签手动加载我的React App,但没有成功。

Internet上有一些教程,其步骤与我相同。

在Django项目中,我该如何以与Node服务器相同的方式加载React App?

谢谢。

0 个答案:

没有答案
相关问题