Question

Static files only served in debug = True but not when False

I have followed the setup procedures here https://www.digitalocean.com/community/tutorials/how-to-set-up-django-with-postgres-nginx-and-gunicorn-on-ubuntu-22-04 and my Django app is running but whenever I turn debug = False it will not load my static files.

Below is my nginx configuration:

server {
    listen 80;
    server_name server_domain_or_IP;

    location = /favicon.ico { access_log off; log_not_found off; }
    location /static/ {
        root /home/sammy/myprojectdir;
    }

    location / {
        include proxy_params;
        proxy_pass http://unix:/run/gunicorn.sock;
    }
}

and my settings.py:

STATIC_URL = 'static/'

STATIC_ROOT = os.path.join(BASE_DIR, 'static')

STATICFILES_DIRS = [os.path.join(BASE_DIR, 'etal/static')]

I have tried my best, researched a lot online, and followed many links but still fruitless.


Submit an answer
Answer a question...

This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

Sign In or Sign Up to Answer

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Hi there,

The has been a similar question asked here:

https://www.digitalocean.com/community/questions/nginx-not-serving-static-files-for-django-app-help

The location /static/ part at sites-available and STATIC_ROOT at settings.py should be same.

For example:

location /static/ {
        alias   /opt/appname/static/;
    }
STATIC_ROOT = '/opt/appname/static/'

After that run:

python manage.py collectstatic

Hope that this helps!

Best,

Bobby

KFSys
Site Moderator
Site Moderator badge
September 19, 2022

Hi @adorablenavykelp,

That’s very weird. Check your permissions and ownership. Most often than not such issues are due to either wrong permissions or wrong file ownership.