140 lines
3.8 KiB
Python
140 lines
3.8 KiB
Python
"""
|
||
Django settings for ourstory project.
|
||
"""
|
||
|
||
import os
|
||
from pathlib import Path
|
||
|
||
BASE_DIR = Path(__file__).resolve().parent.parent
|
||
|
||
SECRET_KEY = os.environ.get(
|
||
"DJANGO_SECRET_KEY",
|
||
"django-insecure-dev-only-change-in-production",
|
||
)
|
||
|
||
DEBUG = os.environ.get("DJANGO_DEBUG", "true").lower() in ("1", "true", "yes")
|
||
|
||
ALLOWED_HOSTS = [
|
||
h.strip()
|
||
for h in os.environ.get("DJANGO_ALLOWED_HOSTS", "localhost,127.0.0.1").split(",")
|
||
if h.strip()
|
||
]
|
||
|
||
INSTALLED_APPS = [
|
||
"django.contrib.admin",
|
||
"django.contrib.auth",
|
||
"django.contrib.contenttypes",
|
||
"django.contrib.sessions",
|
||
"django.contrib.messages",
|
||
"django.contrib.staticfiles",
|
||
"rest_framework",
|
||
"corsheaders",
|
||
"journal",
|
||
]
|
||
|
||
MIDDLEWARE = [
|
||
"django.middleware.security.SecurityMiddleware",
|
||
"whitenoise.middleware.WhiteNoiseMiddleware",
|
||
"corsheaders.middleware.CorsMiddleware",
|
||
"django.contrib.sessions.middleware.SessionMiddleware",
|
||
"django.middleware.common.CommonMiddleware",
|
||
"django.middleware.csrf.CsrfViewMiddleware",
|
||
"django.contrib.auth.middleware.AuthenticationMiddleware",
|
||
"django.contrib.messages.middleware.MessageMiddleware",
|
||
"django.middleware.clickjacking.XFrameOptionsMiddleware",
|
||
]
|
||
|
||
ROOT_URLCONF = "ourstory.urls"
|
||
|
||
TEMPLATES = [
|
||
{
|
||
"BACKEND": "django.template.backends.django.DjangoTemplates",
|
||
"DIRS": [],
|
||
"APP_DIRS": True,
|
||
"OPTIONS": {
|
||
"context_processors": [
|
||
"django.template.context_processors.request",
|
||
"django.contrib.auth.context_processors.auth",
|
||
"django.contrib.messages.context_processors.messages",
|
||
],
|
||
},
|
||
},
|
||
]
|
||
|
||
WSGI_APPLICATION = "ourstory.wsgi.application"
|
||
|
||
_sqlite_path = os.environ.get("SQLITE_PATH")
|
||
DATABASES = {
|
||
"default": {
|
||
"ENGINE": "django.db.backends.sqlite3",
|
||
"NAME": _sqlite_path if _sqlite_path else 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 = "zh-hans"
|
||
|
||
TIME_ZONE = "Asia/Shanghai"
|
||
|
||
USE_I18N = True
|
||
|
||
USE_TZ = True
|
||
|
||
MEDIA_URL = "/media/"
|
||
_media_root = os.environ.get("MEDIA_ROOT")
|
||
MEDIA_ROOT = Path(_media_root) if _media_root else BASE_DIR / "media"
|
||
|
||
STATIC_URL = "static/"
|
||
STATIC_ROOT = BASE_DIR / "staticfiles"
|
||
|
||
STORAGES = {
|
||
"default": {
|
||
"BACKEND": "django.core.files.storage.FileSystemStorage",
|
||
"OPTIONS": {
|
||
"location": str(MEDIA_ROOT),
|
||
"base_url": MEDIA_URL,
|
||
},
|
||
},
|
||
"staticfiles": {
|
||
"BACKEND": "whitenoise.storage.CompressedStaticFilesStorage",
|
||
},
|
||
}
|
||
|
||
DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField"
|
||
|
||
REST_FRAMEWORK = {
|
||
"DEFAULT_RENDERER_CLASSES": [
|
||
"rest_framework.renderers.JSONRenderer",
|
||
],
|
||
"DEFAULT_PARSER_CLASSES": [
|
||
"rest_framework.parsers.JSONParser",
|
||
],
|
||
"DEFAULT_AUTHENTICATION_CLASSES": [],
|
||
"DEFAULT_PERMISSION_CLASSES": [
|
||
"rest_framework.permissions.AllowAny",
|
||
],
|
||
}
|
||
|
||
_cors_origins = os.environ.get(
|
||
"CORS_ALLOWED_ORIGINS",
|
||
"http://localhost:5173,http://127.0.0.1:5173",
|
||
)
|
||
CORS_ALLOWED_ORIGINS = [o.strip() for o in _cors_origins.split(",") if o.strip()]
|
||
CORS_ALLOW_CREDENTIALS = True
|
||
|
||
CSRF_TRUSTED_ORIGINS = [
|
||
o.strip() for o in os.environ.get("CSRF_TRUSTED_ORIGINS", "").split(",") if o.strip()
|
||
]
|
||
# 未配置时至少信任本机 Admin,避免部分环境下保存表单被 CSRF 拦截
|
||
if not CSRF_TRUSTED_ORIGINS:
|
||
CSRF_TRUSTED_ORIGINS = [
|
||
"http://127.0.0.1:8000",
|
||
"http://localhost:8000",
|
||
]
|