Hello, so today I’m writing and explaining about Environment Variables in Django. You might be wondering what is Environment Variables. Well, the Environment variable exists outside of your code as part of your server environment. It can help you by both streamlining and making more secure the process of running your scripts and applications. Things like our secret keys in our settings.py, our database credentials in our settings.py, our secret API keys, and so on are always kept in our environment variables. We use environment variables for improved security and smoother workflows.
Note: Environment Variables is not only applicable to Django, you can use pretty much the same exact thing in any of your python based projects whether it is core Python or Flask. You can do this procedure in any of your Python-based projects.
Now you already have basic information on what Environment Variables are, let’s jump right into it.
We are going to create a ‘.env’ file. What is a .env file? The .env file is a file that actually contains some secrets concerning our project. Like I said above Secret keys, Database Credentials, email passwords, etc are kept in the .env file. So now in your Django project directory, create a file named ‘.env’ (PS: Do not include any extension, just ‘.env’).
So now, you can keep your secret and sensitive keys here. Here I’m going to take my app’s Secret Key and email password from settings.py to the .env file
Now to the next step
PS: Do not store your sensitive keys such as the SECRET_KEY as a string. Unlike in our settings.py file, we store our Secret Keys as strings, but here in the .env file DO NOT store it as strings. Also, make sure there is no space before the equal to ‘=’ sign and after it. E.G
IN OUR Settings.py FILE:
SECRET_KEY=” s*og6c!d4cubukcksn$iksdw#(m!%!4t7m+8q!62^f)s9d1=p8t2op ”
IN OUR .env FILE:
Now we are going to install a python package called Python-decouple. Decouple helps you organize and separate settings from your code so that you can change parameters without having to redeploy them. So we are going to use this package to implement the .env file in our Django project.
So now go ahead and install the package from your Terminal or Bash:
“pip install python-decouple”
Now the next thing is the actual usage. In your settings.py,
1. Import the config object:
‘from decouple import config’
2. Retrieve the configuration parameters; now I kept my secret key and database password in the .env file, so now I’m going to retrieve it.
SECRET_KEY = config (‘SECRET_KEY’)
EMAIL_HOST_PASSWORD = config (‘EMAIL_HOST_PASSWORD’)
Now maybe in your case you did keep other sensitive keys like the Debug status, Email Host, Email keys, API secret keys, and so on, I will show you here how to retrieve them.
DEBUG = config (‘DEBUG’, cast=bool)
EMAIL_HOST = config (‘EMAIL_HOST’, default= ‘localhost’)
EMAIL_PORT = config (‘EMAIL_PORT’, cast=int)
NAME_OF_API_SECRET_KEY = config (‘NAME_OF_API_SECRET_KEY’)
Note: We passed another argument after DEBUG in the Debug config. The argument we passed is cast. The purpose of passing the argument is to inform config the type of data that is passed in. So ‘cast=bool’ tells config that the data we are passing is a Boolean value. The same goes with EMAIL PORT also.
If you use GIT, do not forget to git ignore your file. You can do this by including the ‘.env’ file in your ‘.gitignore’ file.