CSS 在实时 Django 网站上不起作用

CSS 在实时 Django 网站上不起作用

此网站已上线并由 Digital Ocean 托管。我终于让它正常工作了,但是 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

这是 Nginx 的问题。在我的 Jnginx 配置文件中,我设置了错误的静态文件路径

所以我有: /home/django/django_project/django_project/static/

但我需要这个: /home/django/django_project/static/

相关内容