0

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

Ivan Starostin
  • 8,798
  • 5
  • 21
  • 39
Rajat Jog
  • 21
  • 2
  • Did you get this issue sorted out? In case you didn't share your project file structure. Usually, the Static files are contained in any one of your applications within Django. Having them at the base directory caused these issues for me. – Surveyor Jr Jan 29 '22 at 17:14

2 Answers2

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)
user8193706
  • 2,387
  • 2
  • 8
  • 12
0

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

location /static/ {
     ...
}
Rajat Jog
  • 21
  • 2