Skip to content

ahmlvs/react-django-app-example-notes

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 

Repository files navigation

react-django-app-example-notes

  1. create env
python3 -m venv env
source env/bin/activate
  1. install requirements
pip install -r requirements.txt

backend

  1. create empty django project "backend"
django-admin startproject backend
  1. create empty app "api"
cd backend
python3 manage.py startapp api
  1. add django settings
from datetime import timedelta
from dotenv import load_dotenv
import os

load_dotenv()

ALLOWED_HOSTS = ["*"]

REST_FRAMEWORK = {
    "DEFAULT_AUTHENTICATION_CLASSES": (
        "rest_framework_simplejwt.authentication.JWTAuthentication",
    ),
    "DEFAULT_PERMISSION_CLASSES": [
        "rest_framework.permissions.IsAuthenticated",
    ],
}

SIMPLE_JWT = {
    "ACCESS_TOKEN_LIFETIME": timedelta(minutes=30),
    "REFRESH_TOKEN_LIFETIME": timedelta(days=1),
}

INSTALLED_APPS = [
    ...,
    "api",
    "rest_framework",
    "corsheaders",
]

MIDDLEWARE = [
    ...,
    "corsheaders.middleware.CorsMiddleware",
]

CORS_ALLOW_ALL_ORIGINS = True
CORS_ALLOWS_CREDENTIALS = True
  1. make migrations
python3 manage.py makemigrations
python manage.py migrate
  1. run application
python3 manage.py runserver

frontend

  1. create empty react project
npm create vite@latest frontend -- --template react
cd frontend
npm install
  1. install some packages
npm install axios react-router-dom jwt-decode
  1. run application
npm run dev

connect remote postgresql database

  1. add variables to "Backend/.env"

  2. add django settings

DATABASES = {
    "default": {
        # "ENGINE": "django.db.backends.sqlite3",
        # "NAME": BASE_DIR / "db.sqlite3",
        "ENGINE": "django.db.backends.postgresql",
        "NAME": os.getenv("DB_NAME"),
        "USER": os.getenv("DB_USER"),
        "PASSWORD": os.getenv("DB_PWD"),
        "HOST": os.getenv("DB_HOST"),
        "PORT": os.getenv("DB_PORT"),
    }
}
  1. update migare to connect to db
python3 manage.py migrate

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published