News from this site

 Rental advertising space, please contact the webmaster if you need cooperation


+focus
focused

classification  

no classification

tag  

no tag

date  

no datas

Django can't find static files

posted on 2024-12-02 22:07     read(351)     comment(0)     like(28)     collect(3)


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/"

solution


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.



Category of website: technical article > Q&A

Author:qs

link:http://www.pythonblackhole.com/blog/article/247236/e01721ba91bae8891841/

source:python black hole net

Please indicate the source for any form of reprinting. If any infringement is discovered, it will be held legally responsible.

28 0
collect article
collected

Comment content: (supports up to 255 characters)