I am trying to set up a postgres connection and use virtual environment variables for the first time. I created a .env file and placed it in my settings folder but my database variable is missing each time I try to use manage.py runserver. Is there a way to get this to run on windows? Moving the .env file next to my virtual environment did not work either. I also tried typing set DATABASE_URL='postgres://username:password@localhost:5432/authors-live'
did not work either.
Folder Structure:
MASTERDJANGOWITHDOCKER
-env (virtual environment folder)
-authors-src
--manage.py
--.env
--authors_api
---settings
----base.py
----.env
(both .env files are exactly the same)
settings/base.py
import environ
env = environ.Env()
ROOT_DIR = Path(__file__).resolve().parent.parent.parent # need three to get to manage.py level
# points django to where the apps are stored
APPS_DIR = ROOT_DIR / 'core_apps'
BASE_DIR = Path(__file__).resolve().parent.parent
DATABASES = {
'default': env.db('DATABASE_URL')
}
.env file
DATABASE_URL='postgres://username:password@localhost:5432/authors-live'
DJANGO_SECRET_KEY="django-insecure"
error message
...
File "C:\Python39\lib\os.py", line 679, in __getitem__
raise KeyError(key) from None
KeyError: 'DATABASE_URL'
...
File "C:\Users\...\authors-haven-api\authors-src\env\lib\site-packages\environ\environ.py", line 371, in get_value
raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: Set the DATABASE_URL environment variable