Skip to content

Using Environment Variables in Django

In this video, we are going to learn about environment variables. We will learn how to use environment variables to keep your secret information like secret key, debug value, database credentials or any other api keys.

So, whenever you deploy your project, you have to make sure that all your secret informations are safe. Because your api keys, database credentials, secret key are very sensitive informations. You can't take a risk to expose them publicly or store them in your repository.

To deal with this problem, we have something called environment variables. Using environment variables, we can configure the credentials of our application based on our requirements. For example: we can configure separate environment credentials for developement, staging, testing, and for production.

Code Setup

  • Now, lets try to use environment variables in django. But at first, lets setup the project code for this section.

  • So, you need to download the project code files the resources section: DOWNLOAD ZIP FILE

  • Now, extract the folder on the desktop.

  • Then, copy the mysite folder which is the project root directory.

  • Replace the mysite folder of your project with the copied mysite folder. So, if you made any mistake while writing the code in the previous section, then, it won't affect your learning in this section.

  • Open your project in the VS code and in the terminal, activate the virtual environment, and move to the mysite.

  • Run the development server.

Using django-environ

Now, you have to download a package named "django-environ" in order to use environment variables in your django project. You can also use environment varibles without any third party library. But this library has a lot to offer you when you move to production environment. So, lets try to use their functionality insead of manually doing everything on our own.

pip install django-environ

Open your settings.py file and import

import environ
import os

Now, after the BASE DIR line, you have to write

env = environ.Env()
environ.Env.read_env(os.path.join(BASE_DIR, ".env"))

After that, create a .env file in the project directory (not in the repository directory)

In this env file, we will store all our secret credentials like database credentials, secret key or any other api keys.

Now, in the settings.py file, we have to read SECRET KEY from env file instead of hard coding the value.

SECRET_KEY = env("SECRET_KEY")

Also, we will read DEBUG value from env file.

DEBUG = env.bool("DEBUG", False)

Now, set the allowed host value to *.

ALLOWED_HOSTS = ["*"]

Since, we have not configured any domain name so, we don't have any value for allowed host except for localhost. But when you are working on a production grade application, then you have to set this value to some ip address or any domain name.

As of now, we will deploy our project in a ubuntu machine that we will borrow from any cloud provider and they will assign any random IP address to that machine. So, lets keep this value as * as of now to allow django serve request from any ip address and to reduce the complexity of deploying our project.

Now in the .env file, add DEBUG and SECRET KEY [DEBUG=True ] [Get the secret key from this method: from django.core.management.utils import get_random_secret_key]

Here, we are using SQLite database, and you can confiure any mysql or postgres database by providing, by providing suitable db backend, host name, db name, password and port number. But in this course as of now, we will keep using sqlite database.

Once, you have learnt the deployment process, you can configure any sorts of database on your own by reading the documentation.

At last, you have to make a commit and push your changes to the remote repository.

[Commit the changes and push it in the remote repository.]