Django: How to Set-Up Environmental Variable

Django: How to Set-Up Environmental Variable

What is an Environment variable?

For Django projects or apps, An Environment variable is a value that can affect the running processes of a Django app. They are part of the dependencies that initiates enable the continuous running of a Django App. Environment variables are made up of name-value pairs. For an instance the SECRET_KEY= “django-insecure-u15_%xj3^f_t3#w” as found in settings.py file in the root directory of a Django project folder. ‘SECRET_KEY’ is the name, while ‘django-insecure-u15_%xj3^f_t3#w’ is the value. The environment variable can be set outside of the program framework through functionalities built into the operating system, micro-services, or web frameworks.

Why Environment variables?

Using Environment variables, you can ensure the security of secure your apps by storing sensitive bits of code like API Keys and passwords in another file that are not accessible to prying eyes such as hackers on the internet. Pushing sensitive information with your code files to a version control system like GitHub is never a good idea.

Steps to Set-up Environment variable

Although there is more than one way to set up an environment variable in python, the steps highlighted below are what I prefer personally.

Step 1: Install Django Environ

Django-environ gives an avenue to use the Twelve-factor methodology in configuring your Django application with environment variables. Pull up the terminal inside the project directory and type:

pip install Django-environ

Step 2: Create your .env file

In the same directory as settings.py, create a file and name it .env

.env.JPG

Step 3: Declare the environmental variable in .env file in the form of key=value.

SECRET_KEY=django-insecure-u15_%xj3^f_t3wbp5m2iv=qu)zg*5x7*%wldb($^-0px@7!d
DATABASE_NAME=postgresdatabase
DATABASE_USERNAME=honordevop
DATABASE_PASS=codingislife
EMAIL_USER=ogunladestephen20@gmail.com
EMAIL_PASS=we29jaaa

Note: - Declare any other variable in the same form as shown above. Don’t use quotations for either the key or value in the .env file

Step 4: Import and initialize environ in settings.py:

Open the setting.py file and type in:

# Importing environment variables
import environ
# Initialise environment variables
env = environ.Env()
environ.Env.read_env()

Step 5: Replace all referenced variable in your environment variable in the settings.py file as shown below;

SECRET_KEY = env(‘SECRET_KEY’)

And then,

DATABASES = {
‘default’: {
‘ENGINE’: ‘django.db.backends.postgresql_psycopg2’,
‘NAME’: env(‘DATABASE_NAME’),
‘USER’: env(‘DATABASE_USERNAME’),
‘PASSWORD’: env(‘DATABASE_PASS’),
}
}

And then,

EMAIL_HOST_USER = env('EMAIL_USER')
EMAIL_HOST_PASSWORD = env('EMAIL_PASS')

Step 6: Exclude .env by adding .env to .gitignore file

This will help ensure that the .env file is not pushed to the remote server of your version control system.

I hope you enjoy the article, your feedback and likes will be appreciated.