Configuring Environment Variables in Django

Configure environment variables to avoid having sensitive information about the application available in the code.

It is a bad habit to have sensitive information about our application available in the code. This is the case for the SECRET_KEY setting and the database settings in the settings.py file of the project. This is especially bad because we’ve pushed the code to GitHub. Let’s correct this.

Environment variables

An environment variable is a variable whose value is set outside the running code of the program. With Python, we can read files from a .env file. We will use the os library to write the configurations. So, first, let’s create a .env file at the root of the Django project and add the following content:

Get hands-on with 1400+ tech skills courses.