For developing a Django project, you don't need to think about environment variables, but if you want to deploy that project, you need to make sure you separate the environment variable.
For a more detailed description of the separation of environment variables, see this dev.to article
My way of separating the environment variables is to create multiple settings files. I usually have a settings.py
file, a settings-local.py
file (which is not pushed to GitHub or version control) and a settings-ci.py
file (these settings are for continuous integration with GitHub).
# blog_project/settings.py
import os
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = os.environ.get("SECRET_KEY")
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = os.environ.get("DEBUG", "True").lower() == "false"
ALLOWED_HOSTS = (
os.environ.get("ALLOWED_HOSTS").split(",")
if os.environ.get("ALLOWED_HOSTS")
else []
)
# set the User
AUTH_USER_MODEL = "account.User"
# Email server configuration
EMAIL_HOST = "smtp.gmail.com"
EMAIL_HOST_USER = os.environ.get("EMAIL_HOST_USER")
EMAIL_HOST_PASSWORD = os.environ.get("EMAIL_HOST_PASSWORD")
EMAIL_PORT = 587
EMAIL_USE_TLS = True
DEFAULT_FROM_EMAIL = os.environ.get("DEFAULT_FROM_EMAIL")
os.environ.get()
: To develop my Django projects I use PyCharm. In PyCharm you can define your terminal environment variables in the PyCharm settings. I set the DJANGO_SETTINGS_MODULE=blog_project.settings-local
and every terminal I open will automatically use the settings-local.py
file as a settings reference. So I use os.environ.get()
to get the variables defined in the settings-local.py
file.# blog_project/settings-local.py
from blog_project.settings import * # noqa
SECRET_KEY = "super_secret"
DEBUG = True
ALLOWED_HOSTS = ["localhost", "127.0.0.1"]
EMAIL_HOST_USER = "name.lastname@gmail.com"
EMAIL_HOST_PASSWORD = "secret_password"
DEFAULT_FROM_EMAIL = "My Blog"
# only for development!!!
EMAIL_BACKEND = "django.core.mail.backends.console.EmailBackend"
SECRET_KEY
: A SECRET_KEY
, which should be set to a unique, unpredictable value and to a specific Django installation, is used for cryptographic signing.DEBUG
: is a boolean which turns on/off the debug mode of the Django project.ALLOWED_HOSTS
: is a list of strings that represent the host names or domain names that this Django site can serve.EMAIL_HOST_USER
: the user name to be used for the SMTP server defined in EMAIL_HOST
(defined in settings.py
file).EMAIL_HOST_PASSWORD
: the password to be used for the SMTP server defined in EMAIL_HOST
(defined in settings.py
file). When authenticating to the SMTP server, this setting is used in conjunction with EMAIL_HOST_USER. Django won't attempt authentication if either of these settings is empty.DEFAULT_FROM_EMAIL
: This is the default e-mail address for automated correspondence from the site manager. This address, which can be any format valid in the selected email sending protocol, is used in the "From:" header of outgoing emails.EMAIL_BACKEND
: selection of the e-mail backend but in our case only for development purpose. The console backend just writes the emails that would be sent to standard output instead of sending real emails.