Create and setup a new Django-React app
Create and setup a base for a new Django-React app with the following steps:
Backend side (Django):
- New folder for the whole project, then
cd
into it
- Run:
pipenv install django djangorestframework
- Create Django project:
django-admin startproject <project_name>
- cd into the newly created
<project_name>
folder
- Change the Python interpreter in VSCode and also in the terminal (open up a bash terminal, it will automatically activate the venv)
- Create an
api
and frontend
Django app: django-admin startapp <app_name>
- Add the new projects to the
settings.py
file eg.: api.apps.ApiConfig
- Also add the
rest_framework
to the apps for React
- Make migrations (every time there is an update in the database structure):
python manage.py makemigrations
- Migrate:
python manage.py migrate
- Run the Django server:
python manage.py runserver
Frontend side (React):
- cd into the frontend folder and perform all these steps inside this folder
- Create a
templates
folder and within a frontend
folder
- Create a
static
folder and within frontend
, css
and images
folders
- Create an
src
folder and within a components
folder
- Run (in frontend):
npm init -y
- Install packages:
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
- Create the
babel.config.json
file and paste the content from here
- Create the
webpack.config.js
file and paste the content from here
- Add these 2 scripts to the
package.json
file and delete the test
script:
"dev": "webpack --mode development --watch",
"build": "webpack --mode production"
- Create an
index.js
file inside the src
folder as paste:
- Create the
index.html
file inside the templates/frontend
folder and paste the content from here
- Create a
components
folder inside src
and first create an App.js
and then paste the content from here
- In
frontend/views.py
create an index view:
- In
frontend
create a new urls.py
file and add the default path directing to the index view:
- In the main
urls.py
add the default path to include the frontend’s urls:
- Run:
npm run dev
Notes:
- the Django and npm server both has to run simultaneously
- whenever an app is created, it has to be added inside the settings.py file
- if there is a change in the database structure, we have to make migrations
- this folder/component/file structure for the frontend app is not mandatory and the copy-pasted material only serves as a starting point
- webpack is a tool to pack/bundle js codes
- babel takes care of the backward compatibility
- material-ui provides the UI components (bootstrap is also a common alternative)
- we have templates/frondend/index.html as the only html file and inside src/ we have the index.js main file
- it is quite common not to work with the index.js file but to create an app.js component (do everything there) and render it into the index.js file
Links: