Home > Software design >  Static files are not loading for Django product on Digital Ocean
Static files are not loading for Django product on Digital Ocean

Time:01-15

I am about to publish my first Django project on a Digital Ocean droplet. I was following the official Digital Ocean guide to install Django on a droplet (https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-22-04#creating-systemd-socket-and-service-files-for-gunicorn) and everything worked fine but the serving of static files.

Here are key parts of my files:

settings.py

BASE_DIR = Path(__file__).resolve().parent.parent
STATIC_ROOT = os.path.join(BASE_DIR,"static")

STATIC_URL = '/static/'

DEBUG = False

COMPRESS_ROOT = os.path.join(BASE_DIR,"static")

COMPRESS_ENABLED = True

STATICFILES_FINDERS = [
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
    'compressor.finders.CompressorFinder',
]

urls.py

urlpatterns = [
    ...
]   static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Here is my folder structure on digital ocean:

root/
    snap/
    gymtracker/
        gymenv/
        gymtracker/
            gymtrackerapp/
            static/

I see on in the terminal on digital ocean that all static files are in this location:

/root/gymtracker/gymtracker/static/

but when I load the page I get 403 for the static files ( the page loads fine otherwise):

Request Method: GET

Status Code: 403 Forbidden

Thanks so much for your help!

CodePudding user response:

Found the solution.

The Digital Ocean tutorial (https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-22-04#creating-systemd-socket-and-service-files-for-gunicorn) missed one important Nginx config change at the end to make your static resources show up.

After you follow the tutorial you need to edit /etc/nginx/nginx.conf and change the user from www-data to your username (e.g sammy) by running

sudo nano /etc/nginx/nginx.conf
  • Related