I made a .env file in the same directory as my settings.py file and have some environmental variables in there such as: secret_key, database_name, etc. However, it doesn't seem to be reading the database name correctly in the .env file. I feel like I followed the docs, but still get the improperly configured error when pushing to Heroku. It does work when running the server locally though.
settings.py
from pathlib import Path
import os
from datetime import timedelta
import environ
env = environ.Env()
environ.Env.read_env()
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Take environment variables from .env file
environ.Env.read_env(os.path.join(BASE_DIR, '.env'))
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = env('SECRET_KEY')
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = env('DEBUG')
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': env('DATABASE_NAME'),
'USER': env('DATABASE_USER'),
'PASSWORD': env('DATABASE_PASSWORD'),
'HOST': env('DATABASE_HOST'),
'PORT': env('DATABASE_PORT'),
}
}
.env (example)
SECRET_KEY=django-insecure-vdihiodnsdkcndocndcndocdcoidcosjvodjv
DEBUG=True
DATABASE_NAME=vjiojjoj3oj3ioj3
DATABASE_USER=vdijvodivjdivfv
...
error
File "/app/project_name/settings.py", line 94, in <module>
'NAME': env('DATABASE_NAME'),
File "/app/.heroku/python/lib/python3.10/site-packages/environ/environ.py", line 175, in __call__
return self.get_value(
File "/app/.heroku/python/lib/python3.10/site-packages/environ/environ.py", line 371, in get_value
raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: Set the DATABASE_NAME environment variable
django-environ docs: https://github.com/joke2k/django-environ
EDIT: Okay it looks like pushing to Heroku with a .env file is not the way to go. Will be trying to link my github repo with heroku and configuring the vars in the settings. We'll see if that will do it.