Django 静态文件未在实际生产中加载
Django STATIC FILES not loading in actual Production
我尝试了很多方法来解决这个问题,比如添加 whitenoise
中间件,还添加了 STATICFILES_DIRS = [],为 css 添加了 mimetypes
在 settings.py 文件中 CSS/JS 不会加载。
这是我的 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'
...
我还在我的 main urls.py
中添加了以下内容
if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
但 css/js 仍未加载,因此我检查了代码并发现以下内容:
403 Forbidden error for static file
而且来源是空的:
Sources are empty
对于静态文件使用 STATIC_URL
和 STATIC_ROOT
。添加到您的主要 urls.py
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
实际上,在我的 Nginx 服务器文件中,我评论了以下部分并且有效
location /static/ {
...
}
我尝试了很多方法来解决这个问题,比如添加 whitenoise
中间件,还添加了 STATICFILES_DIRS = [],为 css 添加了 mimetypes
在 settings.py 文件中 CSS/JS 不会加载。
这是我的 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'
...
我还在我的 main urls.py
中添加了以下内容if settings.DEBUG:
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
但 css/js 仍未加载,因此我检查了代码并发现以下内容: 403 Forbidden error for static file
而且来源是空的: Sources are empty
对于静态文件使用 STATIC_URL
和 STATIC_ROOT
。添加到您的主要 urls.py
if settings.DEBUG:
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
实际上,在我的 Nginx 服务器文件中,我评论了以下部分并且有效
location /static/ {
...
}