Django won't load admin dashboard css with ngnix

Viewed 28

I have a problem that has been holding me up in development for 1 week and I can't get out of it by seeing other users' answers. My project consists of a backend in Django and a frontend in React, the whole application is Dockerized and is served using ngnix. Now with the current configuration the Django css is not loading, what am I doing wrong? I thank everyone for the help.

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

 }

my Project enter image description here

0 Answers
Related