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

Hey bro don't need to panic. Let's do it.

  1. First things first you have to have the latest version of pip and here is the link to update it https://pip.pypa.io/en/stable/installation/
  2. Make below changes to your settings.py file.
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent

INSTALLED_APPS = [
    'whitenoise.runserver_nostatic', # put it at the top
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    # applications are always at the bottom
    'hotdog_app',
    'corsheaders',
]

MIDDLEWARE = [
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.security.SecurityMiddleware',
    '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',
    'whitenoise.middleware.WhiteNoiseMiddleware', # put it at the bottom
]
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
  1. Run this command to automatically create the staticfiles folder and that is python manage.py collectstatic.

In staticfiles folder, you’ll find two main subfolders: an admin folder, which can be deleted if you’re not using the admin panel, and folders named after each of your apps. Within these app folders, you can add as many CSS and JavaScript files as needed. To enable static file loading, include {% load static %} at the top of your HTML file and add css link in head tag as <link rel="stylesheet" href="{% static 'test_app/css/custom.css' %}">, if you want to add js link you can do that like <script type="module" src="{% static 'test_app/js/main.js' %}"></script>.

Also, could you confirm if you're planning to deploy your application on Vercel or another platform? Most platforms follow a similar process for static file management, but it’s important to be aware of any platform-specific requirements in case issues arise. For more detailed information on configuring WhiteNoise, refer to the official documentation https://whitenoise.readthedocs.io/en/stable/django.html

Back to Top