Django无法找到静态文件

时间:2016-08-09 09:59:47

标签: python django nginx static gunicorn

我试图在Digital Ocean上部署我的Django项目。我已经选择了Django App droplet来使它变得更简单。

然后我只是安装了需求,将django_project替换为我的项目。

问题是静态文件(至少css)根本不起作用。我可以看到网络,但没有css

Settings.py:

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

我已经按照本教程One-Click Django - DigitalOcean

进行了操作

python manage.py collectstatic 我已经尝试重新启动gunicorn

的nginx /位点可用/ 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;
    }
}

编辑:

CSS适用于Django-Admin个页面

重要编辑;

我检查了nginx日志,似乎它试图在错误的路径中找到这些文件。它正在搜索其默认项目路径。

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

2 个答案:

答案 0 :(得分:1)

尝试按如下方式指定STATIC_ROOT:

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',
)

答案 1 :(得分:1)

如果您的日志文件显示nginx正在向服务器查找文件但是从错误的位置查找文件,因为您的nginx文件自上次更改后未更新。

Reload your nginx configuration它应该开始正确地提供文件。