fix: 支持从独立环境变量配置 PostgreSQL 数据库
- 添加 DB_ENGINE, DB_NAME, DB_USER 等环境变量 - 支持 PostgreSQL 和 SQLite 切换 - 云服务器默认使用 PostgreSQL
This commit is contained in:
@@ -86,17 +86,18 @@ WSGI_APPLICATION = 'city_manual.wsgi.application'
|
||||
|
||||
import os
|
||||
|
||||
DATABASE_URL = os.environ.get('DATABASE_URL', '')
|
||||
# 数据库配置 - 支持 PostgreSQL 和 SQLite
|
||||
DB_ENGINE = os.environ.get('DB_ENGINE', '')
|
||||
|
||||
if DATABASE_URL.startswith('postgres'):
|
||||
if DB_ENGINE == 'django.db.backends.postgresql':
|
||||
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],
|
||||
'NAME': os.environ.get('DB_NAME', 'cssc'),
|
||||
'USER': os.environ.get('DB_USER', 'coder'),
|
||||
'PASSWORD': os.environ.get('DB_PASSWORD', ''),
|
||||
'HOST': os.environ.get('DB_HOST', 'localhost'),
|
||||
'PORT': os.environ.get('DB_PORT', '5432'),
|
||||
}
|
||||
}
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user