From 89e8589e87977bb7a815ceffeb05303233b9e515 Mon Sep 17 00:00:00 2001 From: maoshen Date: Sun, 12 Apr 2026 22:06:04 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=94=AF=E6=8C=81=E4=BB=8E=E7=8E=AF?= =?UTF-8?q?=E5=A2=83=E5=8F=98=E9=87=8F=E9=85=8D=E7=BD=AE=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 os 模块导入 - DEBUG 和 ALLOWED_HOSTS 从环境变量读取 - DATABASE_URL 支持 PostgreSQL 和 SQLite - 默认使用 SQLite 便于部署 --- city-manual/backend/city_manual/settings.py | 36 ++++++++++++++------- city-manual/scripts/deploy-via-rsync.sh | 3 +- 2 files changed, 27 insertions(+), 12 deletions(-) diff --git a/city-manual/backend/city_manual/settings.py b/city-manual/backend/city_manual/settings.py index 5a24dc7..34f2544 100644 --- a/city-manual/backend/city_manual/settings.py +++ b/city-manual/backend/city_manual/settings.py @@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/4.2/ref/settings/ """ from pathlib import Path +import os # Build paths inside the project like this: BASE_DIR / 'subdir'. BASE_DIR = Path(__file__).resolve().parent.parent @@ -23,9 +24,9 @@ BASE_DIR = Path(__file__).resolve().parent.parent SECRET_KEY = 'django-insecure-vvuexg2$gadudvj18-24xf3m$7f=8+ugtl&o@r_vgso)@#^$l2' # SECURITY WARNING: don't run with debug turned on in production! -DEBUG = True +DEBUG = os.environ.get('DEBUG', 'True').lower() == 'true' -ALLOWED_HOSTS = ['*'] +ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '*').split(',') # Application definition @@ -83,16 +84,29 @@ WSGI_APPLICATION = 'city_manual.wsgi.application' # Database # https://docs.djangoproject.com/en/4.2/ref/settings/#databases -DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.postgresql', - 'NAME': 'cssc', - 'USER': 'coder', - 'PASSWORD': '825670wl', - 'HOST': '10.2.0.100', - 'PORT': '5432', +import os + +DATABASE_URL = os.environ.get('DATABASE_URL', '') + +if DATABASE_URL.startswith('postgres'): + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.postgresql', + 'NAME': DATABASE_URL.split('/')[-1].split('?')[0], + 'USER': DATABASE_URL.split('@')[0].split('/')[-1].split(':')[0], + 'PASSWORD': DATABASE_URL.split('@')[0].split(':')[-1], + 'HOST': DATABASE_URL.split('@')[1].split(':')[0], + 'PORT': DATABASE_URL.split('@')[1].split(':')[-1].split('?')[0], + } + } +else: + # 默认使用 SQLite + DATABASES = { + 'default': { + 'ENGINE': 'django.db.backends.sqlite3', + 'NAME': BASE_DIR / 'db.sqlite3', + } } -} # Password validation diff --git a/city-manual/scripts/deploy-via-rsync.sh b/city-manual/scripts/deploy-via-rsync.sh index 198d1d9..ee24f02 100755 --- a/city-manual/scripts/deploy-via-rsync.sh +++ b/city-manual/scripts/deploy-via-rsync.sh @@ -64,7 +64,8 @@ DJANGO_SETTINGS_MODULE=config.settings.production DJANGO_SECRET_KEY=cssc-secret-key-$(date +%s) DEBUG=False ALLOWED_HOSTS=cssc.datalibstar.com,127.0.0.1,localhost -DATABASE_URL=postgres://coder:825670wl@10.2.0.100:5432/cssc +# 临时使用 SQLite,后续配置可访问的 PostgreSQL +DATABASE_URL=sqlite:///db.sqlite3 MEDIA_ROOT=/home/ubuntu/city-manual/backend/media STATIC_ROOT=/home/ubuntu/city-manual/backend/static EOF