Secure Sensitive Information in Django on GitHub

Vaibhav Vaibhav Oct 12, 2021 May 22, 2021
Secure Sensitive Information in Django on GitHub

Building projects and software and sharing them with the world on GitHub is a fun and exciting process. We relish to add exciting and compelling features to our applications and push the code to GitHub using git. Still, sometimes we tend to forget about one of the most critical aspects of software development: security.

When building software, we have to ensure that the software is safe and doesn’t have potential loopholes for a data breach.

Django, the web framework, comes with robust security. The authentication system of Django is mighty. Django has a where most of the sensitive data is present. Hence, storing such a file on GitHub could be a dangerous decision.

But don’t worry. There are a few ways to push the code on GitHub and ensure safety at the same time.

Secure Sensitive Information in Django using Environment Variables

Environment variables are some variables whose values are stored outside the program, and they are read from the program to access the information. These variables are stored in a .env file.

First, create a file .env. Then, inside the .gitignore file, add .env. This addition will make sure that the .env file is not uploaded to GitHub.

Now you will need a Python Package to deal with these files. The package name is Decouple. Use the following pip command to install the package.

pip install python-decouple


pip3 install python-decouple

Inside the .env file, add the sensitive information. This information could be your Django project secret key, your database credentials, your email id and password, your API keys, etc. The Decouple Python Package will automatically look for this file inside the Django project.

Store the information in the following manner. Make sure that you follow the same syntax: no spaces included before and after the = sign and no quotation marks added to represent the strings.


As mentioned above, we generally and should keep all the sensitive data in the file. We’ll do the same here as well, but we will not directly add the values. We will read them from this .env we just created.

Inside the, we can retrieve the above values as follows.

import os
from decouple import config

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = config('SECRET_KEY') # Here
DEBUG = config('DEBUG', cast = bool) # Here
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': config("DATABASE_NAME"),  # Here
        'USER': config("DATABASE_USERNAME"),  # Here
        'PASSWORD': config("DATABASE_PASSWORD"),  # Here
        'HOST': config("DATABASE_HOST"),  # Here
        'PORT': config("DATABASE_PORT", cast = int),  # Here
EMAIL = config("EMAIL")

config has a cast parameter that defines to which data type the value should be cast to. By default, Decouple reads all the values as strings. Hence, this cast parameter can be used to cast the values to the desired data type.

That’s it. Now, all the sensitive data is safe in a .env file, and as long as you don’t upload this file to GitHub, the information is secure. Moreover, the project will not break either because the variables are intact where they were present previously. The only thing that has changed is the way we assign them values.

Vaibhav Vaibhav avatar Vaibhav Vaibhav avatar

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.

LinkedIn GitHub