Django不会用ngnix加载管理面板css

rwqw0loc  于 2022-11-26  发布在  Go
关注(0)|答案(1)|浏览(118)

我有一个问题困扰了我一个星期,我无法通过查看其他用户的答案来解决这个问题。我的项目由Django的后端和React的前端组成,整个应用程序是Dockerized的,使用ngnix。现在,在当前的配置下,Django css无法加载,我做错了什么?感谢大家的帮助。

设置.py

# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/4.0/howto/static-files/

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static', 'static')

MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'static', 'media')

网址.py

urlpatterns = [
path('admin/', admin.site.urls),
path('', include('DigitalMapping.urls')),
path('test', test, name='test'),
path('notificationCompletedOperation', NotificationCompletedOperation, 
name='notificationCompletedOperation'),
path('notificationErrorOperation', NotificationErrorOperation, 
name='notificationErrorOperation'),
path('', include('notifications.urls')),
] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

停靠文件

FROM python:3.8.5
   WORKDIR /backend
   COPY ./ ./
   RUN pip install --upgrade pip
   RUN pip install -r requirements.txt
   RUN python manage.py collectstatic
   ENV PYTHONPATH ./backend

默认.conf

upstream django {
server django:8002;
}

server {
listen 8080;

location / {
root /var/www/react;
}

location  ~ ^/(api|admin)/ {
proxy_pass http://django;
proxy_set_header Host $http_host;

  if ($request_method = 'OPTIONS') {
    add_header 'Access-Control-Allow-Origin' '*';
    #
    # Om nom nom cookies
    #
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    #
    # Custom headers and headers various browsers *should* be OK with but aren't
    #
    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X- 
    Requested-With,If-Modified-Since,Cache-Control,Content-Type';
    #
    # Tell client that this pre-flight info is valid for 20 days
    #
    add_header 'Access-Control-Max-Age' 1728000;
    add_header 'Content-Type' 'text/plain charset=UTF-8';
    add_header 'Content-Length' 0;
    return 204;
  }
  if ($request_method = 'POST') {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X- 
 Requested-With,If-Modified-Since,Cache-Control,Content-Type';
 }
 if ($request_method = 'GET') {
    add_header 'Access-Control-Allow-Origin' '*';
    add_header 'Access-Control-Allow-Credentials' 'true';
    add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
    add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X- 
 Requested-With,If-Modified-Since,Cache-Control,Content-Type';
 }
 }

    location /static/ {
    autoindex on;
    autoindex_exact_size off;
    }

 location /media/ {
    autoindex on;
    autoindex_exact_size off;
 }

 }

我的项目enter image description here

xqnpmsa8

xqnpmsa81#

您可以尝试以下顺序-
更多上下文:https://saasitive.com/tutorial/docker-compose-django-react-nginx-let-s-encrypt/
在启动gunicorn之前,调用collectstatic命令创建包含admin css的文件夹:

./manage.py collectstatic --noinput

请注意,它在哪里写出了需要与下面的别名匹配的文件。输出位置是Django settings.py文件中定义的STATIC_ROOT。“164个静态文件复制到'/app/backend/django_static'”
确保你的nginx conf文件中有这个位置,别名对应于输出位置。这里我们将Django STATIC_URLMap到STATIC_ROOT

location /django_static/ {
    autoindex on;
    alias /app/backend/django_static/;
}

将该文件挂载到docker-compose. yml中的前端容器。它应该与上面的别名匹配。通过在前端和后端容器中指定下面的卷,前端容器能够读取后端容器中的www.example.com命令输出的静态文件manage.py。

volumes:
            - static_volume:/app/backend/django_static

现在这些静态文件将被用来设计django管理页面的样式。如果它不工作,请确保查看Web服务器日志,因为如果它找不到admin.css文件,它们会告诉你它在哪里寻找它们

相关问题