Django 不会使用 ngnix 加载管理仪表板 css



我有一个问题已经困扰了我一周的开发,我无法通过查看其他用户的答案来摆脱它。我的项目由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')

urls.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)

Dockerfile

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

default.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;
}
}

我的项目在此处输入图像描述

您可以尝试以下序列-

更多上下文:https://saasitive.com/tutorial/docker-compose-django-react-nginx-let-s-encrypt/

在启动gunicorn之前,调用collectstatic命令创建包含admin-css的文件夹:

./manage.py collectstatic --noinput

注意它在哪里写出文件,因为这需要匹配下面的别名。输出位置是Djangosettings.py文件中定义的STATIC_ROOT。"164个静态文件复制到"/app/backend/djang_static";

确保您在nginx conf中有这个位置,并且别名对应于输出位置。在这里,我们将DjangoSTATIC_URL映射到STATIC_ROOT:

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

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

volumes:
- static_volume:/app/backend/django_static

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

最新更新