Managing development .env in Django
Python-dotenv reads key-value pairs from a .env file and can set them as environment variables. It helps in the development of applications following the 12-factor principles.
Create a .env.example file in the root file.
.env.example
while in development, copy and paste .env.example, new file called .env
.env
SECRET_KEY="your secret key inside quotes"
DB_NAME=db-name
DB_USER=root
DB_PASS=
DB_HOST=localhost
DB_PORT=3306
Update the settings.py file in project
settings.py
from dotenv import load_dotenv
from datetime import timedelta
import os
# load environment variables from a file named .env into the environment of your application.
load_dotenv()
# added
SECRET_KEY = os.environ.get('SECRET_KEY')
# updated the databse
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': os.environ.get('DB_NAME'),
'USER': os.environ.get('DB_USER'),
'PASSWORD': os.environ.get('DB_PASS'),
'HOST': os.environ.get('DB_HOST'),
'PORT': os.environ.get('DB_PORT'),
'OPTIONS': {
'init_command': "SET sql_mode='STRICT_TRANS_TABLES'",
},
}
}
That's it now you're good to go ;)