ImportError:import dj_database_url ImportError:没有名为'dj_database_url'的模块

时间:2017-08-30 15:34:48

标签: django database python-3.x heroku

我正在尝试使用Django + Heroku +所有必要的依赖项来创建我的应用程序。

执行以下步骤后:

migrating an existing django project

但是,当我运行python3 manage.py runserver时,我一直收到此错误:

  

导入dj_database_url   ImportError:没有名为'dj_database_url'的模块

我尝试使用these instructions this

进行修复

这是我的代码:

我导入了dj-database-url

import dj_database_url

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

我添加了以下STATIC资产必需品

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

STATIC_ROOT = os.path.join(PROJECT_ROOT, 'staticfiles')
STATIC_URL = '/static/'

# Extra places for collectstatic to find static files.
STATICFILES_DIRS = (
    os.path.join(PROJECT_ROOT, 'static'),
)

STATICFILES_STORAGE = 'whitenoise.django.GzipManifestStaticFilesStorage'

这是我的requirements.txt文件

  

DJ-数据库-URL == 0.4.2

     

gunicorn == 19.7.1

     

白噪声== 3.3.0

我仍然得到ImportError。我该如何解决?

3 个答案:

答案 0 :(得分:1)

您是否使用

之类的东西安装了库
pip install -r requirements.txt

pip install dj-database-url==0.4.2

.env/bin/pip install dj-database-url==0.4.2

答案 1 :(得分:0)

数据库url用于将数据库与Heroku挂钩/连接。尝试这种方法。

if 'DATABASE_URL' in os.environ:
    DATABASES = {
        'default': dj_database_url.parse(os.environ.get('DATABASE_URL'))
    }
else:
    print("Postgres URL not found, using sqlite instead")
    DATABASES = {
        'default': {
            'ENGINE': 'django.db.backends.sqlite3',
            'NAME': os.path.join(BASE_DIR, 'db.sqlite3'),
        }
    }

另外,检查是否在Heroku设置中将DATABASE_URL添加到了config vars中。此外,还要检查是否在config vars中添加了SECRET_KEY,以及是否正确配置了ALLOWED_HOSTS`` as they have to be states like this in your settings.py``:

SECRET_KEY = os.environ.get("SECRET_KEY")

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = ['<your-app-name>.herokuapp.com', '127.0.0.1']

您的requirement.txt必须看起来像这样:


dj-database-url==0.5.0
Django==1.11.24
django-forms-bootstrap==3.1.0
gunicorn==20.0.4
Pillow==7.0.0
psycopg2-binary==2.8.4
pytz==2019.3
whitenoise==5.0.1

答案 2 :(得分:0)

这对我有用

from django.conf.urls import url
from django.urls import reverse_lazy
from django.contrib.auth.views import PasswordResetView, PasswordResetDoneView, PasswordResetConfirmView, PasswordResetCompleteView

urlpatterns = [
    url(
        'accounts/password_reset/',
        PasswordResetView.as_view(),
        name='password_reset'
    ),
    url(
        'accounts/password_reset_done/',
        PasswordResetDoneView.as_view(),
        name='password_reset_done'
    ),
    url(
        'accounts/password_reset_confirm/',
        PasswordResetConfirmView.as_view(),
        name='password_reset_confirm'
    ),
    url(
        'accounts/password_reset_complete/',
        PasswordResetCompleteView.as_view(),
        name='password_reset_complete'
    ),
]