Skip to content

Commit 7bf5cb7

Browse files
committed
Modify settings for deployment
1 parent 333a080 commit 7bf5cb7

2 files changed

Lines changed: 49 additions & 28 deletions

File tree

requirements.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
asgiref==3.10.0
2+
dj-database-url==3.0.1
23
Django==5.2.7
4+
django-cors-headers==4.9.0
35
django-environ==0.12.0
46
djangorestframework==3.16.1
57
djangorestframework_simplejwt==5.5.1
68
mysqlclient==2.2.7
9+
psycopg2-binary==2.9.11
710
PyJWT==2.10.1
811
python-dotenv==1.1.1
912
sqlparse==0.5.3
1013
tzdata==2025.2
14+
whitenoise==6.11.0

task_manager/settings.py

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,10 @@
1-
"""
2-
Django settings for task_manager project.
3-
4-
Generated by 'django-admin startproject' using Django 5.2.7.
5-
6-
For more information on this file, see
7-
https://docs.djangoproject.com/en/5.2/topics/settings/
8-
9-
For the full list of settings and their values, see
10-
https://docs.djangoproject.com/en/5.2/ref/settings/
11-
"""
121

132
from pathlib import Path
143
from dotenv import load_dotenv
154
from datetime import timedelta
165
import os
176
import environ
18-
7+
import dj_database_url # render and update requirements.txt
198
env = environ.Env(DEBUG=(bool, False))
209
environ.Env.read_env()
2110

@@ -30,10 +19,11 @@
3019
# SECURITY WARNING: keep the secret key used in production secret!
3120
SECRET_KEY = os.getenv('SECRET_KEY')
3221

33-
# SECURITY WARNING: don't run with debug turned on in production!
34-
DEBUG = os.getenv('DEBUG') == 'True'
22+
# don't run with debug turned on in production!
23+
DEBUG = env.bool("DJANGO_DEBUG", default=True)
3524

36-
ALLOWED_HOSTS = ['localhost', '127.0.0.1', '[::1]']
25+
26+
ALLOWED_HOSTS = ['localhost', '127.0.0.1', '[::1]', ".onrender.com"]
3727

3828

3929
# Application definition
@@ -59,7 +49,8 @@
5949
'django.contrib.auth.middleware.AuthenticationMiddleware',
6050
'django.contrib.messages.middleware.MessageMiddleware',
6151
'django.middleware.clickjacking.XFrameOptionsMiddleware',
62-
'corsheaders.middleware.CorsMiddleware'
52+
'corsheaders.middleware.CorsMiddleware',
53+
"whitenoise.middleware.WhiteNoiseMiddleware" # render
6354
]
6455

6556

@@ -86,19 +77,29 @@
8677
# Database
8778
# https://docs.djangoproject.com/en/5.2/ref/settings/#databases
8879

89-
DATABASES = {
90-
'default': {
91-
'ENGINE': 'django.db.backends.mysql',
92-
'NAME': env('DB_NAME'),
93-
'USER': env('DB_USER'),
94-
'PASSWORD': env('DB_PASSWORD'),
95-
'HOST': env('DB_HOST'),
96-
'PORT': env('DB_PORT'),
97-
'OPTIONS': {
98-
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
80+
if DEBUG:
81+
DATABASES = {
82+
'default': {
83+
'ENGINE': 'django.db.backends.mysql',
84+
'NAME': env('DB_NAME'),
85+
'USER': env('DB_USER'),
86+
'PASSWORD': env('DB_PASSWORD'),
87+
'HOST': env('DB_HOST'),
88+
'PORT': env('DB_PORT'),
89+
'OPTIONS': {
90+
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'"
91+
}
9992
}
10093
}
101-
}
94+
else: # render
95+
DATABASES = {
96+
'default': dj_database_url.config(
97+
default=env('postgresql://task_manager_system_hscm_user:lmvHZk9MYhuOWh63eP4q79ZRbrLpxvt3@dpg-d4qiplidbo4c73bslgm0-a.singapore-postgres.render.com/task_manager_system_hscm'),
98+
conn_max_age=600,
99+
ssl_require=True,
100+
)
101+
}
102+
102103

103104

104105
# Password validation
@@ -137,6 +138,7 @@
137138

138139
STATIC_ROOT = '/app/staticfiles'
139140
STATIC_URL = 'static/'
141+
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage" # render
140142

141143
# Default primary key field type
142144
# https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field
@@ -159,11 +161,26 @@
159161
# Nginx → Django HTTPS handling
160162
SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https")
161163

164+
# CORS Handling
162165
CORS_ALLOWED_ORIGINS = [
163166
"http://localhost:3000",
164167
"http://127.0.0.1:3000",
165168
"http://localhost:8000",
166-
"http://localhost:5173"
169+
"http://localhost:5173",
170+
"https://your-frontend.onrender.com"
167171
]
168172

169173
CORS_ALLOW_CREDENTIALS = True
174+
175+
CSRF_TRUSTED_ORIGINS = [
176+
"https://your-backend.onrender.com",
177+
"https://your-frontend.onrender.com",
178+
179+
]
180+
181+
# Cookies Authentication on Frontend
182+
if not DEBUG:
183+
SECURE_SSL_REDIRECT = True
184+
SESSION_COOKIE_SECURE = True
185+
CSRF_COOKIE_SECURE = True
186+
CSRF_COOKIE_SAMESITE = "None"

0 commit comments

Comments
 (0)