Django/Whitenoise collectstatic causing Permission Denied Error

I have been struggling with this for weeks and have hit a brick wall. Im trying to deploy a Django App and Im using Whitenoise to handle Static Data. When I run collectstatic I get “Permissions Denied” Error. How do I run collectstatic so that it can run without Permission problems. Django is installed in a virtualenv so running it as sudo doesn’t work.

The error happens when using Whitenoise to handle static file using collectstatic when DEBUG is True or False and whether I use the Whitenoise STATICFILES_STORAGE or the Django version.

$ python3 manage.py collectstatic   
“ File "/home/artillery/a1_lounge/web_dev/webdesign_biz/Portfolio/React/Vite/django/.venv/lib/python3.10/site-packages/django/core/files/storage/filesystem.py", line 106, in _save
    fd = os.open(full_path, self.OS_OPEN_FLAGS, 0o666)
PermissionError: [Errno 13] Permission denied: '/home/artillery/a1_lounge/web_dev/webdesign_biz/Portfolio/React/Vite/django/hotdog/staticfiles/admin/js/admin/RelatedObjectLookups.js'”  

settings.py

INSTALLED_APPS = [
    'hotdog_app',
    'corsheaders',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'whitenoise.runserver_nostatic', # Whitenoise serves static in Dev and Prod
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware', # Enable Whitenoise
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATICFILES_STORAGE = "whitenoise.storage.CompressedManifestStaticFilesStorage"

Normally creating a file will give standard permissions eg

(.venv) $ touch test.txt  
-rw-rw-r--  1 artillery artillery    0 Nov  8 13:00 text.txt  

On first run of collectstatic

(.venv) $ python3 manage.py collectstatic  
PermissionError: [Errno 13] Permission denied: '/home/artillery/a1_lounge/web_dev/courses/CS50/Project-1/wiki/staticfiles/admin/js/vendor/select2/i18n/ne.js'  

staticfiles folder created with no read access and locked

d-wx-wx-wT  7 artillery artillery 4.0K Nov  8 12:49 staticfiles  

All Files created within the staticfiles folder are locked and created with very limited permissions

--w --- -r-T  1 artillery artillery  12K Nov  8 12:49 staticfiles.json  

then I ran the following commands set the permissions manually

(.venv) $ sudo find staticfiles -type d -exec chmod 775 {} +  
(.venv) $ sudo find staticfiles -type f -exec chmod 665 {} +  

This will set the permissions correct, but Every time I run collectstatic I get Permissions error for a different file. Does anyone know how to fix this?

What the best way to get collectstatic to work with Whitenoise or how to get around these permission errors? Thanks

Ive tried setting the permissions of the folders and files manually and then rerunning collectstatic again, but it fails with a Permission Error each time with a new file, and would need to do this thousands of times to get through them all.

Tried all the variations of the STATICFILES_STORAGE for Django and Whitenoise

Tried with DEBUG=False and DEBUG=True

Вернуться на верх