Milano
Milano

Reputation: 18705

Django can't find static files

I'm trying to deploy my Django project on Digital Ocean. I've chosen Django App droplet to make it more simple.

Then I've simply installed requirements, replace their django_project to my project.

The problem is that static files (at least css) doesn't work at all. I can see the web but no css.

Settings.py:

STATIC_ROOT = '/home/django/SolutionsForLanguages_2/SolutionsForLanguagesApp/static'
STATIC_URL = '/static/'

I've followed this tutorial One-Click Django - DigitalOcean

Did python manage.py collectstatic I've already tried to restart gunicorn.

nginx/sites-available/django:

upstream app_server {
    server 127.0.0.1:9000 fail_timeout=0;
}

server {
    listen 80 default_server;
    listen [::]:80 default_server ipv6only=on;

    root /usr/share/nginx/html;
    index index.html index.htm;

    client_max_body_size 4G;
    server_name _;

    keepalive_timeout 5;

    # Your Django project's media files - amend as required
    location /media  {
        alias /home/django/SolutionsForLanguages_2/SolutionsForLanguagesApp/media;
    }

    # your Django project's static files - amend as required
    location /static {
        alias /home/django/SolutionsForLanguages_2/SolutionsForLanguagesApp/static; 
    }

    # Proxy the static assests for the Django Admin panel
    location /static/admin {
       alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
    }

    location / {
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_redirect off;
        proxy_pass http://app_server;
    }
}

EDIT:

CSS works for Django-Admin pages

IMPORTANT EDIT;

I've checked nginx log and it seems that it tries to find these files in wrong path. It is searching in their default project path.

2016/08/09 06:31:29 [error] 998#0: *140 open() "/home/django/django_project/django_project/static/css/bootstrap.min.css" failed (2: No such file or directory), client: 89.103.231.4, server: _, request: "GET /static/css/bootstrap.min.css HTTP/1.1", host: "37.139.20.125", referrer: "http://37.139.20.125/"
2016/08/09 06:31:29 [error] 998#0: *142 open() "/home/django/django_project/django_project/static/css/jumbotron.css" failed (2: No such file or directory), client: 89.103.231.4, server: _, request: "GET /static/css/jumbotron.css HTTP/1.1", host: "37.139.20.125", referrer: "http://37.139.20.125/"
2016/08/09 06:31:29 [error] 998#0: *140 open() "/home/django/django_project/django_project/static/css/dropdown.css" failed (2: No such file or directory), client: 89.103.231.4, server: _, request: "GET /static/css/dropdown.css HTTP/1.1", host: "37.139.20.125", referrer: "http://37.139.20.125/"
2016/08/09 06:31:29 [error] 998#0: *143 open() "/home/django/django_project/django_project/static/css/bootstrap-theme.min.css" failed (2: No such file or directory), client: 89.103.231.4, server: _, request: "GET /static/css/bootstrap-theme.min.css HTTP/1.1", host: "37.139.20.125", referrer: "http://37.139.20.125/"
2016/08/09 06:31:30 [error] 998#0: *140 open() "/home/django/django_project/django_project/static/img/hourglass.png" failed (2: No such file or directory), client: 89.103.231.4, server: _, request: "GET /static/img/hourglass.png HTTP/1.1", host: "37.139.20.125", referrer: "http://37.139.20.125/"
2016/08/09 06:31:30 [error] 998#0: *142 open() "/home/django/django_project/django_project/static/img/value.png" failed (2: No such file or directory), client: 89.103.231.4, server: _, request: "GET /static/img/value.png HTTP/1.1", host: "37.139.20.125", referrer: "http://37.139.20.125/"
2016/08/09 06:31:30 [error] 998#0: *143 open() "/home/django/django_project/django_project/static/img/people.png" failed (2: No such file or directory), client: 89.103.231.4, server: _, request: "GET /static/img/people.png HTTP/1.1", host: "37.139.20.125", referrer: "http://37.139.20.125/"
2016/08/09 06:31:30 [error] 998#0: *144 open() "/home/django/django_project/django_project/static/img/howitworks.png" failed (2: No such file or directory), client: 89.103.231.4, server: _, request: "GET /static/img/howitworks.png HTTP/1.1", host: "37.139.20.125", referrer: "http://37.139.20.125/"
2016/08/09 06:31:30 [error] 998#0: *140 open() "/home/django/django_project/django_project/static/img/languages.png" failed (2: No such file or directory), client: 89.103.231.4, server: _, request: "GET /static/img/languages.png HTTP/1.1", host: "37.139.20.125", referrer: "http://37.139.20.125/"
2016/08/09 06:31:30 [error] 998#0: *142 open() "/home/django/django_project/django_project/static/img/logos.png" failed (2: No such file or directory), client: 89.103.231.4, server: _, request: "GET /static/img/logos.png HTTP/1.1", host: "37.139.20.125", referrer: "http://37.139.20.125/"
2016/08/09 06:31:30 [error] 998#0: *143 open() "/home/django/django_project/django_project/static/img/pricing.png" failed (2: No such file or directory), client: 89.103.231.4, server: _, request: "GET /static/img/pricing.png HTTP/1.1", host: "37.139.20.125", referrer: "http://37.139.20.125/"
2016/08/09 06:31:30 [error] 998#0: *144 open() "/home/django/django_project/django_project/static/img/fcb.png" failed (2: No such file or directory), client: 89.103.231.4, server: _, request: "GET /static/img/fcb.png HTTP/1.1", host: "37.139.20.125", referrer: "http://37.139.20.125/"
2016/08/09 06:31:30 [error] 998#0: *140 open() "/home/django/django_project/django_project/static/img/twitter.png" failed (2: No such file or directory), client: 89.103.231.4, server: _, request: "GET /static/img/twitter.png HTTP/1.1", host: "37.139.20.125", referrer: "http://37.139.20.125/"
2016/08/09 06:31:30 [error] 998#0: *142 open() "/home/django/django_project/django_project/static/img/linkedin.png" failed (2: No such file or directory), client: 89.103.231.4, server: _, request: "GET /static/img/linkedin.png HTTP/1.1", host: "37.139.20.125", referrer: "http://37.139.20.125/"
2016/08/09 06:31:30 [error] 998#0: *145 open() "/home/django/django_project/django_project/static/img/helpful.png" failed (2: No such file or directory), client: 89.103.231.4, server: _, request: "GET /static/img/helpful.png HTTP/1.1", host: "37.139.20.125", referrer: "http://37.139.20.125/"

Upvotes: 3

Views: 2982

Answers (2)

Matt Seymour
Matt Seymour

Reputation: 9395

If your log file is showing nginx looking for files to server but from the wrong location then it because you nginx file has not be updated since your last change.

Reload your nginx configuration and it should start serving files correctly.

Upvotes: 1

Kishan Mehta
Kishan Mehta

Reputation: 2678

Try specifying STATIC_ROOT as following:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
# Note that number of times os.path.dirname is used depends on 
# to reach top of your project directory. here
# Static Files
STATIC_ROOT = join(os.path.dirname(BASE_DIR), 'staticfiles')
STATICFILES_DIRS = [join(os.path.dirname(BASE_DIR), 'static'), ]
STATIC_URL = '/static/'
STATICFILES_FINDERS = (
    'django.contrib.staticfiles.finders.FileSystemFinder',
    'django.contrib.staticfiles.finders.AppDirectoriesFinder',
)

Upvotes: 1

Related Questions