From 483f150e7acd21d0eb47116483cbdae6e3f90708 Mon Sep 17 00:00:00 2001 From: Andrey Smakotin Date: Mon, 22 Dec 2025 20:45:52 +0300 Subject: [PATCH] feat(static): improve static files handling and permissions in Docker - Add script to set correct permissions on static files after collectstatic - Introduce collectstatic command in entrypoint with permission fixing - Add WhiteNoise middleware for efficient static file serving without DB access - Configure WhiteNoise static files storage backend in settings - Set STATIC_ROOT path properly for Docker container environment - Add fallback static files serving in Django urls for production without nginx - Enhance inventory_detail.html scripts to log errors if JS files or components fail to load - Add whitenoise package to requirements for static file serving support --- docker/entrypoint.sh | 17 ++++++++++++++ .../inventory/inventory/inventory_detail.html | 21 +++++++++++++++-- myproject/myproject/settings.py | 23 ++++++++++++++----- myproject/myproject/urls.py | 6 +++++ myproject/myproject/urls_public.py | 6 +++++ myproject/requirements.txt | 1 + 6 files changed, 66 insertions(+), 8 deletions(-) diff --git a/docker/entrypoint.sh b/docker/entrypoint.sh index b93685c..a359174 100755 --- a/docker/entrypoint.sh +++ b/docker/entrypoint.sh @@ -138,6 +138,12 @@ run_migrations() { echo "Collecting static files..." python manage.py collectstatic --noinput + # Устанавливаем права ПОСЛЕ collectstatic + echo "Setting permissions on static files..." + STATIC_ROOT="/app/myproject/staticfiles" + find "$STATIC_ROOT" -type d -exec chmod 755 {} \; 2>/dev/null || true + find "$STATIC_ROOT" -type f -exec chmod 644 {} \; 2>/dev/null || true + echo "Ensuring public tenant exists..." python /app/docker/create_public_tenant.py } @@ -225,6 +231,17 @@ case "$1" in run_migrations create_superuser ;; + collectstatic) + wait_for_postgres + setup_directories + echo "Collecting static files..." + python manage.py collectstatic --noinput + echo "Setting permissions on static files..." + STATIC_ROOT="/app/myproject/staticfiles" + find "$STATIC_ROOT" -type d -exec chmod 755 {} \; 2>/dev/null || true + find "$STATIC_ROOT" -type f -exec chmod 644 {} \; 2>/dev/null || true + echo "Static files collected and permissions set." + ;; shell) exec python manage.py shell ;; diff --git a/myproject/inventory/templates/inventory/inventory/inventory_detail.html b/myproject/inventory/templates/inventory/inventory/inventory_detail.html index 771c3a2..18afc95 100644 --- a/myproject/inventory/templates/inventory/inventory/inventory_detail.html +++ b/myproject/inventory/templates/inventory/inventory/inventory_detail.html @@ -284,12 +284,25 @@ {% endif %} - - + +