Skip to content

Commit 061a6b1

Browse files
authored
Refactor Django settings for improved clarity
1 parent 4cb65fa commit 061a6b1

File tree

1 file changed

+48
-59
lines changed

1 file changed

+48
-59
lines changed

settings.py

Lines changed: 48 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,57 +1,46 @@
1-
"""
2-
Django settings for ons_trading project.
3-
4-
Generated by 'django-admin startproject' using Django 5.2.1.
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-
131
import os
142
from pathlib import Path
15-
import dj_database_url # Import dj_database_url
3+
import dj_database_url
164

17-
# Build paths inside the project like this: BASE_DIR / 'subdir'.
5+
# -------------------------------------------------------------
6+
# BASE DIRECTORY
7+
# -------------------------------------------------------------
188
BASE_DIR = Path(__file__).resolve().parent.parent
199

10+
# -------------------------------------------------------------
11+
# SECURITY SETTINGS
12+
# -------------------------------------------------------------
13+
# Get the SECRET_KEY from Render's environment variable
14+
SECRET_KEY = os.environ.get('SECRET_KEY', 'django-insecure-default-key')
2015

21-
# Quick-start development settings - unsuitable for production
22-
# See https://docs.djangoproject.com/en/5.2/howto/deployment/checklist/
23-
24-
# SECURITY WARNING: keep the secret key used in production secret!
25-
# Get the SECRET_KEY from a Render environment variable
26-
SECRET_KEY = os.environ.get('SECRET_KEY', 'django-insecure-n&a6vut67vp5ysh09jvsts&6%m5e*b_#hr82iuw(1dk^d18yok')
27-
28-
# SECURITY WARNING: don't run with debug turned on in production!
29-
# Get DEBUG status from a Render environment variable (defaults to False)
16+
# Get DEBUG status from Render (defaults to False in production)
3017
DEBUG = os.environ.get('DEBUG', 'False') == 'True'
3118

32-
# Get the ALLOWED_HOSTS from Render's environment variable
19+
# ALLOWED_HOSTS: Add Render's external hostname automatically
3320
ALLOWED_HOSTS = []
3421
RENDER_EXTERNAL_HOSTNAME = os.environ.get('RENDER_EXTERNAL_HOSTNAME')
3522
if RENDER_EXTERNAL_HOSTNAME:
3623
ALLOWED_HOSTS.append(RENDER_EXTERNAL_HOSTNAME)
3724

38-
39-
# Application definition
40-
25+
# -------------------------------------------------------------
26+
# INSTALLED APPS
27+
# -------------------------------------------------------------
4128
INSTALLED_APPS = [
4229
"django.contrib.admin",
4330
"django.contrib.auth",
4431
"django.contrib.contenttypes",
4532
"django.contrib.sessions",
4633
"django.contrib.messages",
4734
"django.contrib.staticfiles",
48-
'onsapp',
35+
'onsapp', # Your custom app
4936
]
5037

38+
# -------------------------------------------------------------
39+
# MIDDLEWARE
40+
# -------------------------------------------------------------
5141
MIDDLEWARE = [
5242
"django.middleware.security.SecurityMiddleware",
53-
# Add WhiteNoise middleware right after SecurityMiddleware
54-
'whitenoise.middleware.WhiteNoiseMiddleware',
43+
"whitenoise.middleware.WhiteNoiseMiddleware", # Required for Render
5544
"django.contrib.sessions.middleware.SessionMiddleware",
5645
"django.middleware.common.CommonMiddleware",
5746
"django.middleware.csrf.CsrfViewMiddleware",
@@ -60,8 +49,14 @@
6049
"django.middleware.clickjacking.XFrameOptionsMiddleware",
6150
]
6251

52+
# -------------------------------------------------------------
53+
# URL CONFIGURATION
54+
# -------------------------------------------------------------
6355
ROOT_URLCONF = "ons_trading.urls"
6456

57+
# -------------------------------------------------------------
58+
# TEMPLATES
59+
# -------------------------------------------------------------
6560
TEMPLATES = [
6661
{
6762
"BACKEND": "django.template.backends.django.DjangoTemplates",
@@ -78,25 +73,25 @@
7873
},
7974
]
8075

76+
# -------------------------------------------------------------
77+
# WSGI APPLICATION
78+
# -------------------------------------------------------------
8179
WSGI_APPLICATION = "ons_trading.wsgi.application"
8280

83-
84-
# Database
85-
# https://docs.djangoproject.com/en/5.2/ref/settings/#databases
86-
87-
# This setup uses dj_database_url to read the DATABASE_URL env var from Render
81+
# -------------------------------------------------------------
82+
# DATABASE CONFIGURATION
83+
# -------------------------------------------------------------
84+
# Uses SQLite locally, but switches to PostgreSQL on Render automatically
8885
DATABASES = {
8986
'default': dj_database_url.config(
90-
# Use SQLite locally if DATABASE_URL is not set
9187
default=f'sqlite:///{BASE_DIR / "db.sqlite3"}',
9288
conn_max_age=600,
9389
)
9490
}
9591

96-
97-
# Password validation
98-
# https://docs.djangoproject.com/en/5.2/ref/settings/#auth-password-validators
99-
92+
# -------------------------------------------------------------
93+
# PASSWORD VALIDATION
94+
# -------------------------------------------------------------
10095
AUTH_PASSWORD_VALIDATORS = [
10196
{
10297
"NAME": "django.contrib.auth.password_validation.UserAttributeSimilarityValidator",
@@ -112,32 +107,26 @@
112107
},
113108
]
114109

115-
116-
# Internationalization
117-
# https://docs.djangoproject.com/en/5.2/topics/i18n/
118-
110+
# -------------------------------------------------------------
111+
# INTERNATIONALIZATION
112+
# -------------------------------------------------------------
119113
LANGUAGE_CODE = "en-us"
120-
121114
TIME_ZONE = 'Asia/Kolkata'
122-
123-
USE_I1N = True
124-
115+
USE_I18N = True
125116
USE_TZ = True
126117

118+
# -------------------------------------------------------------
119+
# STATIC FILES (CSS, JavaScript, Images)
120+
# -------------------------------------------------------------
121+
STATIC_URL = "/static/"
127122

128-
# Static files (CSS, JavaScript, Images)
129-
# https://docs.djangoproject.com/en/5.2/howto/static-files/
130-
131-
STATIC_URL = "static/"
132-
133-
# This is the directory where collectstatic will gather all static files.
123+
# This is crucial for Render deployment (fixes ImproperlyConfigured error)
134124
STATIC_ROOT = BASE_DIR / 'staticfiles'
135125

136-
# Enable WhiteNoise to serve static files efficiently.
126+
# Enable WhiteNoise compression and caching
137127
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
138128

139-
140-
# Default primary key field type
141-
# https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field
142-
129+
# -------------------------------------------------------------
130+
# DEFAULT AUTO FIELD
131+
# -------------------------------------------------------------
143132
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"

0 commit comments

Comments
 (0)