0

This is how my settings.py looks like:

import os
import dotenv

dotenv_file = os.path.join(BASE_DIR, ".env")
if os.path.isfile(dotenv_file):
    dotenv.load_dotenv(dotenv_file)

# UPDATE secret key
SECRET_KEY = os.environ['SECRET_KEY']

This is how my .env file looks like:

SECRET_KEY="TOPSECRETKEY"

When running python manage.py migrate, it returns KeyError: 'SECRET_KEY'

2 Answers2

1

I figured it out, (for future reference) My .env file was in the same folder as settings.py and it needed to be in the same folder as manage.py

0

You can use the python-decouple library.

A resume from its doc:

Decouple helps you to organize your settings so that you can change parameters without having to redeploy your app.

It also makes it easy for you to:

  1. store parameters in ini or .env files;
  2. define comprehensive default values;
  3. properly convert values to the correct data type;
  4. have only one configuration module to rule all your instances.

You can read more in the library documentation itself https://pypi.org/project/python-decouple/#usage