搞定docker所需的通过环境变量控制

This commit is contained in:
2025-11-15 15:20:26 +08:00
parent e1152bdc86
commit 046b649aec
5 changed files with 48 additions and 9 deletions

View File

@@ -22,12 +22,12 @@ BASE_DIR = Path(__file__).resolve().parent.parent
# See https://docs.djangoproject.com/en/5.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-p^*6tak7wy1z#bw__#o^s5hsydearm=(-s(km!-61j2(#)*+-t'
SECRET_KEY = os.environ.get('DJANGO_SECRET_KEY', 'django-insecure-p^*6tak7wy1z#bw__#o^s5hsydearm=(-s(km!-61j2(#)*+-t')
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
DEBUG = os.environ.get('DJANGO_DEBUG', 'True').lower() == 'true'
ALLOWED_HOSTS = ["127.0.0.1", "localhost"]
ALLOWED_HOSTS = os.environ.get('DJANGO_ALLOWED_HOSTS', '127.0.0.1,localhost').split(',')
# Application definition
@@ -47,6 +47,7 @@ INSTALLED_APPS = [
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
@@ -121,6 +122,7 @@ USE_TZ = True
# https://docs.djangoproject.com/en/5.2/howto/static-files/
STATIC_URL = 'static/'
STATIC_ROOT = BASE_DIR / 'staticfiles'
# Media files (uploaded images)
MEDIA_URL = '/media/'
@@ -134,6 +136,8 @@ SESSION_COOKIE_SECURE = False if DEBUG else True
CSRF_COOKIE_SECURE = False if DEBUG else True
CSRF_COOKIE_SAMESITE = 'Lax'
CSRF_TRUSTED_ORIGINS = os.environ.get('DJANGO_CSRF_TRUSTED_ORIGINS', '').split(',') if os.environ.get('DJANGO_CSRF_TRUSTED_ORIGINS') else []
X_FRAME_OPTIONS = 'DENY'
# Default primary key field type
@@ -142,9 +146,12 @@ X_FRAME_OPTIONS = 'DENY'
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
# Elasticsearch configuration
_ES_URL = os.environ.get('ELASTICSEARCH_URL', 'http://localhost:9200')
if not (_ES_URL.startswith('http://') or _ES_URL.startswith('https://')):
_ES_URL = 'http://' + _ES_URL
ELASTICSEARCH_DSL = {
'default': {
'hosts': 'localhost:9200'
'hosts': _ES_URL
},
}