Django CSS不在现场工作

时间:2017-09-27 03:09:22

标签: python django

此网站已上线并托管数字海洋。我终于让它正常工作,但css不适用于该网站?这是我设置的,没有错误,只是css不起作用。

我在settings.py中有这个:

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

STATICFILES_DIRS = [
    STATIC_DIR,
]

这是我的项目urls.py:

from django.conf.urls import url
from django.contrib import admin
from django.conf.urls import include
from blog import views
from users import views
from feed import views
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    url(r'^admin/', admin.site.urls),
    url(r'^$',views.HomeView.as_view(),name='index'),
    url(r'^user/',include('users.urls',namespace='users')),
    url(r'^feed/',include('feed.urls',namespace='feed')),
    url(r'^blog/',include('blog.urls',namespace='blog')),
    url(r'^accounts/', include('allauth.urls')),
]

文件结构:

- django_project
    - /allauth/ 
    - /blog/
    - /django_project/
    - /feed/
    - manage.py
    - /media/
    - req.txt
    - /static/
        - /css/
    - /templates/
    - /users/
    - gunicorn.socket

我已经python manage.py collect static

这是Nginx配置:

upstream app_server {
    server unix:/home/django/gunicorn.socket 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/django_project/django_project/media;
    }

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

    # Proxy the static assests for the Django Admin panel


    location / {
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $host;
            proxy_redirect off;
            proxy_buffering off;

            proxy_pass http://app_server;
    }

}

1 个答案:

答案 0 :(得分:0)

点击安装后的数字海洋水滴做这些事情。移除您放置的任何内容STATIC_URLSTATIC_ROOTSTATIC_DIRSTATICFILES_DIRS然后

项目settings.py

 STATIC_ROOT = '/home/django/django_project/django_project/static'
 STATIC_URL = '/static/'
  

然后

删除或评论/ etc / nginx / sites-available / django中的这些行

#location /static/admin {
#   alias /usr/lib/python2.7/dist-packages/django/contrib/admin/static/admin/;
#}

访问我的网站,了解此问题missing static files on digitalocean server using nginx的详细答案。

  

现在做

 python manage.py collectstaic
 service gunicorn restart
 service nginx restart