From 5dceef61b999cfe0abc45ed371f487c2e428a4c6 Mon Sep 17 00:00:00 2001 From: east4ming Date: Mon, 11 Feb 2019 19:19:54 +0800 Subject: [PATCH] =?UTF-8?q?20190211:django=20settings.py=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9DATABASES,=20=E5=A2=9E=E5=8A=A0=E5=86=85=E9=83=A8IP.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: east4ming --- .idea/workspace.xml | 44 +++++++++++++++++++++++++++++----------- learning_log/database.py | 28 +++++++++++++++++++++++++ learning_log/settings.py | 9 ++++---- 3 files changed, 65 insertions(+), 16 deletions(-) create mode 100644 learning_log/database.py diff --git a/.idea/workspace.xml b/.idea/workspace.xml index 1a3e5dd..b7d6939 100644 --- a/.idea/workspace.xml +++ b/.idea/workspace.xml @@ -2,6 +2,7 @@ + @@ -24,8 +25,8 @@ - - + + @@ -38,8 +39,8 @@ @@ -100,6 +101,7 @@ @@ -141,7 +143,7 @@ - + - @@ -265,8 +274,9 @@ + - + @@ -303,12 +313,12 @@ - - @@ -545,10 +555,20 @@ + + + + + + + + + + - - + + diff --git a/learning_log/database.py b/learning_log/database.py new file mode 100644 index 0000000..af8fbd9 --- /dev/null +++ b/learning_log/database.py @@ -0,0 +1,28 @@ +import os + +from django.conf import settings + +engines = { + 'sqlite': 'django.db.backends.sqlite3', + 'postgresql': 'django.db.backends.postgresql_psycopg2', +} + + +def config(): + service_name = os.getenv('DATABASE_SERVICE_NAME', '').upper().replace('-', + '_') + if service_name: + engine = engines.get(os.getenv('DATABASE_ENGINE'), engines['sqlite']) + else: + engine = engines['sqlite'] + name = os.getenv('DATABASE_NAME') + if not name and engine == engines['sqlite']: + name = os.path.join(settings.BASE_DIR, 'db.sqlite3') + return { + 'ENGINE': engine, + 'NAME': name, + 'USER': os.getenv('DATABASE_USER'), + 'PASSWORD': os.getenv('DATABASE_PASSWORD'), + 'HOST': os.getenv('{}_SERVICE_HOST'.format(service_name)), + 'PORT': os.getenv('{}_SERVICE_PORT'.format(service_name)), + } diff --git a/learning_log/settings.py b/learning_log/settings.py index 3e7931a..812ef4d 100644 --- a/learning_log/settings.py +++ b/learning_log/settings.py @@ -79,11 +79,10 @@ # Database # https://docs.djangoproject.com/en/2.1/ref/settings/#databases +from . import database + DATABASES = { - 'default': { - 'ENGINE': 'django.db.backends.sqlite3', - 'NAME': os.path.join(BASE_DIR, 'db.sqlite3'), - } + 'default': database.config() } # Password validation @@ -130,3 +129,5 @@ BOOTSTRAP3 = { 'include_jquery': True, } + +INTERNAL_IPS = ['127.0.0.1']