Skip to content

Commit 4f2b05f

Browse files
committed
use .env for setting
1 parent 6d4b595 commit 4f2b05f

File tree

4 files changed

+31
-10
lines changed

4 files changed

+31
-10
lines changed

.env

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
SECRET_KEY=abc
2+
DEBUG=1
3+
ALLOWED_HOSTS=127.0.0.1,localhost
4+
DB_NAME=chat_data
5+
DB_USER=postgres
6+
DB_PASSWORD=password
7+
DB_HOST=localhost
8+
DB_POST=5432

core/settings.py

+14-9
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,17 @@
2121
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
2222

2323
# SECURITY WARNING: keep the secret key used in production secret!
24-
SECRET_KEY = 'django-insecure-r-6oa@6p)#jku)f4)-t!)&g%t6h^-3r7y-tgd&k61vsc)^ex2i'
24+
SECRET_KEY = os.environ.get('SECRET_KEY', 'django-insecure-secret-key')
2525

2626
# SECURITY WARNING: don't run with debug turned on in production!
27-
DEBUG = True
28-
29-
ALLOWED_HOSTS = []
27+
the_debug = os.environ.get('DEBUG', '1') == '1'
28+
if the_debug:
29+
DEBUG = True
30+
ALLOWED_HOSTS = []
31+
else:
32+
DEBUG = False
33+
default_host = "127.0.0.1,localhost"
34+
ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', default_host).split(',')
3035

3136

3237
# Application definition
@@ -93,11 +98,11 @@
9398
DATABASES = {
9499
'default': {
95100
'ENGINE': 'django.db.backends.postgresql_psycopg2',
96-
'NAME': 'chat_data',
97-
'USER': 'postgres',
98-
'PASSWORD': 'password',
99-
'HOST': 'localhost',
100-
'POST': 5432,
101+
'NAME': os.environ.get('DB_NAME', 'chat_data'),
102+
'USER': os.environ.get('DB_USER', 'postgres'),
103+
'PASSWORD': os.environ.get('DB_PASSWORD', 'password'),
104+
'HOST': os.environ.get('DB_HOST', 'localhost'),
105+
'POST': int(os.environ.get('DB_POST', '5432')),
101106
'TEST': {
102107
'NAME': os.path.join(BASE_DIR, 'db_test.sqlite3')
103108
}

manage.py

+7
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22
"""Django's command-line utility for administrative tasks."""
33
import os
44
import sys
5+
import dotenv
6+
import pathlib
57

68

79
def main():
810
"""Run administrative tasks."""
11+
DOT_ENV_PATH = pathlib.Path() / '.env'
12+
if DOT_ENV_PATH.exists():
13+
dotenv.read_dotenv(DOT_ENV_PATH)
14+
else:
15+
print("No .env found, be sure to make it!")
916
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'core.settings')
1017
try:
1118
from django.core.management import execute_from_command_line

requirements.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,5 @@ djangorestframework==3.13.1
66
Markdown==3.3.6
77
django-filter==21.1
88
psycopg2==2.9.3
9-
django-adminlte-3==0.1.6
9+
django-adminlte-3==0.1.6
10+
django-dotenv==1.4.2

0 commit comments

Comments
 (0)