|
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 | | -""" |
12 | 1 |
|
13 | 2 | from pathlib import Path |
14 | 3 | from dotenv import load_dotenv |
15 | 4 | from datetime import timedelta |
16 | 5 | import os |
17 | 6 | import environ |
18 | | - |
| 7 | +import dj_database_url # render and update requirements.txt |
19 | 8 | env = environ.Env(DEBUG=(bool, False)) |
20 | 9 | environ.Env.read_env() |
21 | 10 |
|
|
30 | 19 | # SECURITY WARNING: keep the secret key used in production secret! |
31 | 20 | SECRET_KEY = os.getenv('SECRET_KEY') |
32 | 21 |
|
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) |
35 | 24 |
|
36 | | -ALLOWED_HOSTS = ['localhost', '127.0.0.1', '[::1]'] |
| 25 | + |
| 26 | +ALLOWED_HOSTS = ['localhost', '127.0.0.1', '[::1]', ".onrender.com"] |
37 | 27 |
|
38 | 28 |
|
39 | 29 | # Application definition |
|
59 | 49 | 'django.contrib.auth.middleware.AuthenticationMiddleware', |
60 | 50 | 'django.contrib.messages.middleware.MessageMiddleware', |
61 | 51 | 'django.middleware.clickjacking.XFrameOptionsMiddleware', |
62 | | - 'corsheaders.middleware.CorsMiddleware' |
| 52 | + 'corsheaders.middleware.CorsMiddleware', |
| 53 | + "whitenoise.middleware.WhiteNoiseMiddleware" # render |
63 | 54 | ] |
64 | 55 |
|
65 | 56 |
|
|
86 | 77 | # Database |
87 | 78 | # https://docs.djangoproject.com/en/5.2/ref/settings/#databases |
88 | 79 |
|
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 | + } |
99 | 92 | } |
100 | 93 | } |
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 | + |
102 | 103 |
|
103 | 104 |
|
104 | 105 | # Password validation |
|
137 | 138 |
|
138 | 139 | STATIC_ROOT = '/app/staticfiles' |
139 | 140 | STATIC_URL = 'static/' |
| 141 | +STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage" # render |
140 | 142 |
|
141 | 143 | # Default primary key field type |
142 | 144 | # https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field |
|
159 | 161 | # Nginx → Django HTTPS handling |
160 | 162 | SECURE_PROXY_SSL_HEADER = ("HTTP_X_FORWARDED_PROTO", "https") |
161 | 163 |
|
| 164 | +# CORS Handling |
162 | 165 | CORS_ALLOWED_ORIGINS = [ |
163 | 166 | "http://localhost:3000", |
164 | 167 | "http://127.0.0.1:3000", |
165 | 168 | "http://localhost:8000", |
166 | | - "http://localhost:5173" |
| 169 | + "http://localhost:5173", |
| 170 | + "https://your-frontend.onrender.com" |
167 | 171 | ] |
168 | 172 |
|
169 | 173 | 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