Add debugging for environment variables and Django settings

- Fix production.py settings to properly read environment variables with defaults
- Add debug_settings management command to troubleshoot environment variable issues
- Add debugging output to entrypoint.sh to see what settings are loaded
- This will help identify why ALLOWED_HOSTS and CSRF_TRUSTED_ORIGINS may not be working
This commit is contained in:
amitrana01 2025-09-11 18:04:23 +05:30
parent 31ab604b7c
commit 23f9778964
5 changed files with 53 additions and 2 deletions

View File

View File

@ -0,0 +1,44 @@
import os
from django.core.management.base import BaseCommand
from django.conf import settings
class Command(BaseCommand):
help = 'Debug Django settings and environment variables'
def handle(self, *args, **options):
self.stdout.write(
self.style.SUCCESS('🔍 Debug Information for Django Settings')
)
# Check environment variables
env_vars = [
'DJANGO_SETTINGS_MODULE',
'DEBUG',
'ALLOWED_HOSTS',
'CSRF_TRUSTED_ORIGINS',
'SECRET_KEY',
'SECURE_SSL_REDIRECT',
]
self.stdout.write('\n📝 Environment Variables:')
for var in env_vars:
value = os.environ.get(var, 'NOT SET')
if var == 'SECRET_KEY' and value != 'NOT SET':
value = f"{value[:10]}...***" # Hide secret key
self.stdout.write(f" {var} = {value}")
# Check Django settings
self.stdout.write('\n⚙️ Django Settings:')
try:
self.stdout.write(f" DEBUG = {settings.DEBUG}")
self.stdout.write(f" ALLOWED_HOSTS = {settings.ALLOWED_HOSTS}")
self.stdout.write(f" CSRF_TRUSTED_ORIGINS = {getattr(settings, 'CSRF_TRUSTED_ORIGINS', 'NOT SET')}")
self.stdout.write(f" SECURE_SSL_REDIRECT = {getattr(settings, 'SECURE_SSL_REDIRECT', 'NOT SET')}")
self.stdout.write(f" SETTINGS MODULE = {settings.SETTINGS_MODULE}")
except Exception as e:
self.stdout.write(
self.style.ERROR(f"Error reading settings: {e}")
)
self.stdout.write('\n✅ Debug completed!')

View File

@ -3,9 +3,13 @@ Production settings for Django project.
""" """
from .base import * from .base import *
DEBUG = False DEBUG = config('DEBUG', default=False, cast=bool)
ALLOWED_HOSTS = config('ALLOWED_HOSTS', cast=lambda v: [s.strip() for s in v.split(',')]) # Override ALLOWED_HOSTS with production default if not set
ALLOWED_HOSTS = config('ALLOWED_HOSTS', default='localhost,127.0.0.1', cast=lambda v: [s.strip() for s in v.split(',')])
# CSRF trusted origins for production (inherits from base.py but can be overridden)
CSRF_TRUSTED_ORIGINS = config('CSRF_TRUSTED_ORIGINS', default='', cast=lambda v: [s.strip() for s in v.split(',') if s.strip()])
SECURE_BROWSER_XSS_FILTER = True SECURE_BROWSER_XSS_FILTER = True
SECURE_CONTENT_TYPE_NOSNIFF = True SECURE_CONTENT_TYPE_NOSNIFF = True

View File

@ -24,6 +24,9 @@ python manage.py setup_social_apps || {
echo "Skipping Tailwind build at runtime (already built during Docker build)..." echo "Skipping Tailwind build at runtime (already built during Docker build)..."
# Note: Tailwind CSS is built during the Docker build process to avoid permission issues # Note: Tailwind CSS is built during the Docker build process to avoid permission issues
echo "🔍 Debugging Django settings..."
python manage.py debug_settings
echo "Collecting static files..." echo "Collecting static files..."
# Handle potential permission issues with Docker volumes # Handle potential permission issues with Docker volumes
# Try to collect static files, but don't fail if it doesn't work since we have build-time static files # Try to collect static files, but don't fail if it doesn't work since we have build-time static files