feat: configure environment variables, CORS, and PostgreSQL database settings for backend-frontend integration

This commit is contained in:
Your Name
2026-04-09 12:39:42 +05:30
parent 17b8356eac
commit 4bdb1ee80d
2 changed files with 20 additions and 4 deletions
+2
View File
@@ -9,6 +9,7 @@ DB_PORT=5432
DEBUG=True DEBUG=True
SECRET_KEY=django-secret-key SECRET_KEY=django-secret-key
ALLOWED_HOSTS=localhost,127.0.0.1 ALLOWED_HOSTS=localhost,127.0.0.1
CORS_ALLOWED_ORIGINS=http://localhost:5173,http://127.0.0.1:5173
# EMAIL # EMAIL
EMAIL_HOST=localhost EMAIL_HOST=localhost
@@ -16,3 +17,4 @@ EMAIL_PORT=1025
# FRONTEND # FRONTEND
VITE_API_URL=http://localhost:8000 VITE_API_URL=http://localhost:8000
FRONTEND_PORT=5173
+18 -4
View File
@@ -10,20 +10,25 @@ For the full list of settings and their values, see
https://docs.djangoproject.com/en/6.0/ref/settings/ https://docs.djangoproject.com/en/6.0/ref/settings/
""" """
import os
from pathlib import Path from pathlib import Path
import environ
# Build paths inside the project like this: BASE_DIR / 'subdir'. # Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent BASE_DIR = Path(__file__).resolve().parent.parent
env = environ.Env()
# find .env in root
environ.Env.read_env(os.path.join(BASE_DIR.parent, ".env"))
# Quick-start development settings - unsuitable for production # Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/6.0/howto/deployment/checklist/ # See https://docs.djangoproject.com/en/6.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret! # SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = "django-insecure-rj_!92m9m5o7&&g5q=uj#=+7@h6q16o4l-7^u2ilz014j%oo-t" SECRET_KEY = env("SECRET_KEY")
# SECURITY WARNING: don't run with debug turned on in production! # SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True DEBUG = env("DEBUG")
ALLOWED_HOSTS = [] ALLOWED_HOSTS = []
@@ -37,9 +42,12 @@ INSTALLED_APPS = [
"django.contrib.sessions", "django.contrib.sessions",
"django.contrib.messages", "django.contrib.messages",
"django.contrib.staticfiles", "django.contrib.staticfiles",
"rest_framework", # for API
"corsheaders", # for API and Frontend connect
] ]
MIDDLEWARE = [ MIDDLEWARE = [
"corsheaders.middleware.CorsMiddleware", # allow frontend to connect
"django.middleware.security.SecurityMiddleware", "django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware", "django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware", "django.middleware.common.CommonMiddleware",
@@ -74,11 +82,17 @@ WSGI_APPLICATION = "config.wsgi.application"
DATABASES = { DATABASES = {
"default": { "default": {
"ENGINE": "django.db.backends.sqlite3", "ENGINE": "django.db.backends.postgresql",
"NAME": BASE_DIR / "db.sqlite3", "NAME": env("DB_NAME"),
"USER": env("DB_USER"),
"PASSWORD": env("DB_PASSWORD"),
"HOST": env("DB_HOST"),
"PORT": env("DB_PORT"),
} }
} }
CORS_ALLOWED_ORIGINS = env.list("CORS_ALLOWED_ORIGINS")
# Password validation # Password validation
# https://docs.djangoproject.com/en/6.0/ref/settings/#auth-password-validators # https://docs.djangoproject.com/en/6.0/ref/settings/#auth-password-validators