简体   繁体   English

无法连接到Heroku中的Django数据库

[英]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 . 我正在尝试在Heroku中部署Django应用程序,并使用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: 该应用程序在本地运行良好(使用python manage.py runserver ),但heroku local web命令和Heroku部署均失败,并显示以下消息:

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 但是,当我进入Heroku的外壳并尝试安装该软件包时,它表示已满足要求

~ $ 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. 附带说明一下,我还有一个名为django_hosts模块,该模块使用并且没有任何问题。 Here is my requirements.txt file: 这是我的requirements.txt文件:

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 : 这是production.py设置文件中使用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. 我以为也许我需要在dj_databse_url下添加django_hosts ,但是没有用。

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. 我针对此问题的解决方案是将依赖项添加到我的Pipfile中,然后重新生成Pipfile.lock。 This was necessary despite requirements.txt already having the dependency listed. 尽管requirements.txt已经列出了依赖项,但这是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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