If you've stumbled upon my article, I assume you are working on a Django project and are wondering how to secure your project information, more importantly, security keys. If so, you've come to the right place as I am about to teach you the best method of doing it. More often than not, I see many of my software engineer friends miss a couple of key points when developing Django projects, and that is, not hiding their and other keys. This is a crucial part of the security of Django as any information exposed can revoke a project. SECRET_KEY OAuth Let's begin. Suppose we have just started a brand new Django project. Just in case, here is the format: django-admin startproject project Now that we have created a new project, let's navigate into the root directory, that being , and into the file. project/ settings.py On the 23rd line of the code, you will find a variable titled . SECRET_KEY os BASE_DIR = os.path.dirname... SECRET_KEY = import # Build paths inside the project like this: os.path.join(BASE_DIR, ...) # SECURITY WARNING: keep the secret key used in production secret! 'ek0@9u(zemu^+%*-z3!&y9mu_7u+edg9%)c%423mdoec-mi*' Here we see that, for the purpose of this blog post, my Django security key is exposed. Now, I will introduce to you a method that will ensure you never expose your project's private keys ever again, dotenv. In your project terminal, type pip3 install python-dotenv This will install a dotenv requirement that will be used to retrieve your secret keys from a file only you have access to. Next, in your file, C & V (copy and paste) the following two lines: settings.py dotenv load_dotenv load_dotenv() from import Afterward, in the root of your project, create a file titled which will serve as your environment variable secret storage for your project. .env In the , you will declare your variables with an sign and paste their information as such: .env = SECRET_KEY=ek0@ u(zemu^+%*-z3!&y9mu_7u+edg9%)c% mdoec-mi* # .env 9 423 Next, in your , you will retrieve the key as follows: settings.py SECRET_KEY = str(os.getenv( )) # settings.py 'SECRET_KEY' What this line does is make the os (operating system) get the file and bring in the data for the following key: . .env SECRET_KEY To ensure no one receives access to the file, it is a general protocol to put your file in the to make sure it won't be committed to GitHub. .env .env .gitignore If you were using any other keys, such as OAuth keys, the method would work the same. For example, here I will implement an OAuth key to use the Twitter OAuth method. TWITTER_OAUTH_KEY = str(os.getenv( )) # settings.py 'TWITTER_OAUTH_KEY' and retrieve the key from my environment file, TWITTER_OAUTH_KEY=[twitter-oauth-key-here] # .env If you would like to follow my software engineering path, feel free to follow me on . GitHub Also published at https://dev.to/vladyslavnua/how-to-protect-your-django-secret-and-oauth-keys-53fl