简体   繁体   English

Django 中的 ReactJs 返回

[英]ReactJs in Django return

I am new to Reactify Django.我是 Reactify Django 的新手。 Actually, i am trying to have React JS to be rendered instead of HTML page.实际上,我正在尝试渲染 React JS 而不是 HTML 页面。 I have following:我有以下内容:

views.py视图.py

from django.shortcuts import render

def home(request):
    return render (request, 'home.html')

home.html主页.html

<html>
    <head>
        {% load staticfiles %}
        <script src="{% static '/App.js' %}"></script>
    </head>
    <body>
        <p>Hello World Home</p>
        {% block content %}{% endblock %}
    </body>
</html>

App.js应用程序.js

import React from 'react';
import logo from './logo.svg';
import './App.css';

function App() {
  return (
    <div className="App">
      Hello to react app!
    </div>
  );
}

export default App;

settings.py设置.py

STATIC_URL = '/static/'

STATICFILES_DIRS = (
    os.path.join(SETTINGS_PATH, 'frontend/src'),
)

I am not sure, if i am understanding the concept correctly.我不确定,如果我正确理解这个概念。 But i am looking for returning the react rendered html in django.I have my react app installed in frontend directory in the same folder.但我正在寻找在 django 中返回反应呈现的 html。我将我的反应应用程序安装在同一文件夹的前端目录中。 Any clarification, will be greatly appreciated.任何澄清,将不胜感激。

You need to enclose the script in Django template blocks so something like -您需要将脚本包含在 Django 模板块中,例如 -


{% block scripts %}
    <script src="{% static '/App.js' %}"></script>
{% endblock %}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM