python - 生产模式和部署的 Setting.py 配置
问题描述
我想要做的是在 settings.py 中进行所有必要的更改以用于生产目的。当我设置 DEBUG=True 时,一切正常,但是当我设置 DEBUG=False 时,这让我感到非常疲倦和沮丧。我一直在努力很多天,但无法弄清楚。设置DEBUG = False,静态文件运行,有些不运行,但媒体文件完全停止工作,我在某些页面中收到服务器错误(500)。而且,我知道修复在settings.py 但不知道怎么做?
import os
from pathlib import Path
BASE_DIR = Path(__file__).resolve().parent.parent
SECRET_KEY = ')osa2y(^uk4sdghs+#(14if-)b1&6_uo@(h#0c%sci^a#!(k@z'
DEBUG = False
ALLOWED_HOSTS = ['dimensionalillusions.herokuapp.com','127.0.0.1']
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
#CUSTOM APPS
'EHub.apps.EhubConfig',
'EBlog.apps.EblogConfig',
'EDashboard.apps.EdashboardConfig',
'mptt',
'ckeditor',
'taggit',
]
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
]
ROOT_URLCONF = 'Dimensionalillusions.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'templates'),],
'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 = 'Dimensionalillusions.wsgi.application'
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
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
STATIC_URL = '/staticfiles/'
STATICFILES_DIRS=[
os.path.join(BASE_DIR, 'staticfiles')
]
STATIC_ROOT=os.path.join(BASE_DIR, 'static')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
MEDIA_ROOT =os.path.join(BASE_DIR, 'staticfiles/mediafiles')
MEDIA_URL ='/mediafiles/'
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
EMAIL_HOST = 'smtp.gmail.com'
EMAIL_USE_TLS = True
EMAIL_PORT = 587
EMAIL_HOST_USER = ''
EMAIL_HOST_PASSWORD = ''
解决方案
添加MEDIA_URL
和MEDIA_ROOT
到您的项目urls.py
from django.conf.urls.static import static
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
推荐阅读
- php - 如何在原生 php 中使用 laravel 模型/存储库
- java - 无法为某些设备生成 FCM 令牌
- vbscript - 获取磁盘信息并输出到一行
- android - 如何使用组集合中的 ID 数组查询用户集合
- javascript - How can I create Collapsible Column header in Data Table?
- c# - ERROR !!! OpenQA.Selenium.DriverServiceNotFoundException is showing even tho i have installed all the drivers required
- vue.js - How to add dynamically v-model and it's initial values to existed html elements by vuejs
- c# - 找不到或打开 PDB 文件,调试本机代码
- python - How do I pass a variable defined as (Entry.get()) from one function to another?
- python - 哈希算法(sha-512)不返回固定长度的位