Question

I can't get Django admin css loaded using nginx server

Hi I am trying to serve statics using Django and Nginx on a VPS to get my project live. My settings.py includes following.

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/
STATIC_ROOT = os.path.join(BASE_DIR, 'static', 'static')

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static', 'media')

My project folder includes: static ----static ----media ----templates

In Nginx I got following:

upstream gunicorn_server {
  # fail_timeout=0 means we always retry an upstream even if it failed
  # to return a good HTTP response (in case the Unicorn master nukes a
  # single worker for timing out).
  server 127.0.0.1:9000 fail_timeout=0;
}

server {

    listen   80;
    server_name example.com;
	keepalive_timeout 5;
    client_max_body_size 4G;
	
    access_log /home/projectuser/logs/nginx-access.log;
    error_log /home/projectuser/logs/nginx-error.log;

    location /static/ {
        alias   /venv/project/static/static;
    }
   
    location /media/ {
        alias   /venv/project/static/media;
    }
    location / {
       
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
       
        if (!-f $request_filename) {
            proxy_pass http://gunicorn_server;
            break;
        }
    }

}

I can’t serve static no matter what and admin panel css is not showing up.

Please advise asap.

Show comments

Submit an answer

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.

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in Q&A, subscribe to topics of interest, and get courses and tools that will help you grow as a developer and scale your project or business.

I had the same issue, my css was working for my actual site but not the admin page. To solve this follow the steps:

  1. go to your command (e.g. puTTy, nginx) and type: nano /etc/nginx/sites-available/django
  2. it will show you three file directories for media, static and admin static 2.5) quit the nano window by pressing ctrl X, don’t save any changes made (in case you accidentally made a change)
  3. for admin static make sure the static file is in the directory shown, if not ,copy paste the admin static files found in …\Python2.7\Lib\site-packages\django\contrib\admin\static\admin

It should sort it out.

The problem is caused because it is looking for the static files of admin in the wrong place - so just put those files where its looking for it.

Thanks for help but it was the path issue in Nginx.