ReactJs in Django return

284 Views Asked by At

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.

1

There are 1 best solutions below

0
On

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


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