Create and setup a base for a new Django-React app with the following steps:
cd
into itpipenv install django djangorestframework
django-admin startproject <project_name>
<project_name>
folderapi
and frontend
Django app: django-admin startapp <app_name>
settings.py
file eg.: api.apps.ApiConfig
rest_framework
to the apps for Reactpython manage.py makemigrations
python manage.py migrate
python manage.py runserver
templates
folder and within a frontend
folderstatic
folder and within frontend
, css
and images
folderssrc
folder and within a components
foldernpm init -y
npm i webpack webpack-cli @babel/core babel-loader @babel/preset-env @babel/preset-react react react-dom --save-dev
npm i @material-ui/core @babel/plugin-proposal-class-properties react-router-dom @material-ui/icons
babel.config.json
file and paste the content from herewebpack.config.js
file and paste the content from herepackage.json
file and delete the test
script:
"dev": "webpack --mode development --watch",
"build": "webpack --mode production"
index.js
file inside the src
folder as paste:import App from './components/App'
index.html
file inside the templates/frontend
folder and paste the content from herecomponents
folder inside src
and first create an App.js
and then paste the content from herefrontend/views.py
create an index view:def index(request, *args, **kwargs):
return render(request, "frontend/index.html")
frontend
create a new urls.py
file and add the default path directing to the index view:from django.urls import path
from .views import index
urlpatterns = [
path("", index),
]
urls.py
add the default path to include the frontend’s urls:path("", include("frontend.urls"))
npm run dev
Links: