简体   繁体   中英

Can't connect to Django database in Heroku

I am trying to deploy a Django application in Heroku and connect it to a database using dj-database-url . The app runs fine locally (with python manage.py runserver ) but both the heroku local web command and the Heroku deployment fail with the message:

9:47:06 PM web.1 |  ModuleNotFoundError: No module named 'dj_database_url'

However, when I go into Heroku's shell and try to install the package, it says that the requirement is already satisfied

~ $ pip install dj-database-url
Requirement already satisfied: dj-database-url in ./.heroku/python/lib/python3.6/site-packages

As a side note, I have another module called django_hosts which I use and doesn't have any problems. Here is my requirements.txt file:

dj-database-url==0.5.0
Django==2.1.1
django-hosts==3.0
psycopg2==2.7.5
pytz==2018.5

And here are the lines in production.py settings file that uses dj_database_url :

import dj_database_url
...
db_from_env = dj_database_url.config()
DATABASES['default'].update(db_from_env)

I don't know if it helps, but here are my installed apps:

I

NSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',

    # third party
    'django_hosts',

    # custom apps
    'analytics',
    'shortener',
]

I thought that maybe I need to add dj_databse_url underneath django_hosts , but it didn't work.

If you need any more information, please let me know in the comments. Cheers!

My solution for this issue was to add the dependency to my Pipfile and then re-generate Pipfile.lock. This was necessary despite requirements.txt already having the dependency listed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM