简体   繁体   中英

ReactJs in Django return

I am new to Reactify Django. Actually, i am trying to have React JS to be rendered instead of HTML page. I have following:

views.py

from django.shortcuts import render

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

home.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

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

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. Any clarification, will be greatly appreciated.

You need to enclose the script in Django template blocks so something like -


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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