'Django STATIC FILES not loading in actual Production

I have tried many things to solve this like adding whitenoise middleware, also added STATICFILES_DIRS = [], added mimetypes for css in settings.py file CSS/JS Won't load.

Here is my settings.py

from pathlib import Path
import environ
import mimetypes

mimetypes.add_type("text/css", ".css", True)

BASE_DIR = Path(__file__).resolve().parent.parent

MEDIA_ROOT = BASE_DIR / 'media'
MEDIA_URL = '/media/'

STATIC_URL = '/static/'
STATIC_ROOT = BASE_DIR / 'static'

env = environ.Env()
environ.Env.read_env()

STRIPE_PUB_KEY = env('STRIPE_PUB_KEY') 

DEBUG = True

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    'users',
    'addresses',
    'orders',
    'feedback',
    'message',
    'gifts_coupons',
    'core',
    'product',
    'fulfillment',
    'cart',
    'discounts',
    'stripe_pay',
    
    'rest_framework',
    'rest_framework.authtoken',
    'phonenumber_field',
    'django_filters',
    'creditcards',
    'mptt',
    'corsheaders',
    'import_export',
    'django_inlinecss',
]

IMPORT_EXPORT_USE_TRANSACTIONS = True

AUTH_USER_MODEL = 'users.User'
CORS_ALLOW_ALL_ORIGINS = True 

CORS_ALLOWED_ORIGINS = [
    'https://estreetmart.in',
    'https://estreetmart.sg',
    'https://ims.estreetmart.in',
    'http://localhost:8000',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'corsheaders.middleware.CorsMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
    'orders.middlewares.cart_middleware',
]

ROOT_URLCONF = 'estreetmart.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'estreetmart.wsgi.application'
...

I also added following in my main urls.py

if settings.DEBUG:
    urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

but still css/js is not loading so I inspected the code and found following: 403 Forbidden error for static file

and also sources is empty: Sources are empty



Solution 1:[1]

Actually in my Nginx server file I commented following section and it worked

location /static/ {
     ...
}

Solution 2:[2]

For static files use STATIC_URL and STATIC_ROOT. Add to your main urls.py

if settings.DEBUG:
    urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rajat Jog
Solution 2 user8193706