简体   繁体   中英

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

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'

# If this is used then `CORS_ALLOWED_ORIGINS` will not have any effect
CORS_ALLOW_ALL_ORIGINS = True 


# TODO: Change urls while going online

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'

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': '',
        'USER': '',
        'PASSWORD': '',
        'HOST': 'localhost',
        'PORT': '5432',
    },
}

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True

MEDIA_ROOT = BASE_DIR / 'media'

MEDIA_URL = '/media/'

STATIC_URL = '/static/'

STATIC_ROOT = BASE_DIR / 'static'

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

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)

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

location /static/ {
     ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM