I used dotenv this time, so make a note so that you don't forget it.
By using dotenv, you can set the DB password and information you don't want to see often in .env and read it via settings.py. Do not upload this .env when deploying. You can upload files to GitHub without showing environment variables.
** Installation method **
$ pip install python-dotenv
** Creating an .env file **
Write environment variables to this file. Target the .env file with .gitignore.
USER="username"
PASSWORD = "password"
** Creating settings.py **
import os
from os.path import join, dirname
from dotenv import load_dotenv
load_dotenv(verbose=True)
dotenv_path = join(dirname(__file__), '.env')
load_dotenv(dotenv_path)
USER = os.environ.get("USER")
PASSWORD = os.environ.get("PASSWORD")
main.py
import settings
USER = settings.USER
PASSWORD = settings.PASSWORD
Please use it as a reference.
Recommended Posts