Tom
Tom

Reputation: 414

Django settings: DATABASE_URL is not working

I just created a test application in Heroku so that I can stay in the same Django project, but quickly switch back and forth between connecting to my production database and my testing app database. I created an environment variable on my laptop using export:TEST_DATABASE_URL="...", but even with this below code I am still connected to my production database when I run my Django project on localhost. Does anyone know how i can accomplish this?

# ~~~ PROD SETTINGS ~~~
# DATABASE_URL = os.environ['DATABASE_URL']
# DEBUG = 'False'

# ~~~ TEST SETTINGS ~~~
DATABASE_URL = os.environ['TEST_DATABASE_URL']
DEBUG = 'True'


# tried commenting this code out so it doesn't use the local sqlite file
# DATABASES = { # Use this to use local test DB # todo: prod doesn't havea access to django_session...
#     'default': {
#         'ENGINE': 'django.db.backends.sqlite3',
#         'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
#     }
# }

Procfile:

release: python3 manage.py migrate
web: daphne django_project.asgi:application --port $PORT --bind 0.0.0.0 -v2
worker: python3 manage.py runworker channels --settings=django_project.settings -v2

Upvotes: 0

Views: 1844

Answers (1)

Tom
Tom

Reputation: 414

I found the answer. Even though I was setting DATABASE_URL = os.environ['DATABASE_URL'] in settings.py, Django ignored that. When running the app locally I had to use export DATABASE_URL={my database credential} in my ubuntu terminal for my localhost to use my test database

Upvotes: 2

Related Questions