首页 > 解决方案 > 在 django heroku 部署中,postgres 数据库出现错误:DATABASES 配置不正确。请提供名称值

问题描述

嗨,我是 Heroku 的新手,我正在尝试在网站上部署我的 django 应用程序。我遇到的问题在标题中。连接到我的 postgres 数据库似乎有问题。在我的本地环境中,数据库工作得很好,这个问题似乎只出现在 heroku 部署中。我已经将 DATABASE_URL 添加为 Heroku 上的配置变量。该应用程序部署但只是说内部服务器错误,这是heroku给我的错误日志:

 raise ImproperlyConfigured(
2020-12-14T23:41:58.543920+00:00 app[web.1]: django.core.exceptions.ImproperlyConfigured: settings.DATABASES is improperly configured. Please supply the NAME value.
2020-12-14T23:41:58.544131+00:00 app[web.1]: 10.148.121.8 - - [14/Dec/2020:23:41:58 +0000] "GET / HTTP/1.1" 500 0 "-" "-"
2020-12-14T23:41:58.545021+00:00 heroku[router]: at=info method=GET path="/" host=premtablelive.herokuapp.com request_id=aa3e9556-0490-4690-8b0b-d414e69a0305 fwd="24.190.19.51" dyno=web.1 connect=1ms service=5ms status=500 bytes=244 protocol=https

我不确定是什么导致了这个问题我没有在我的代码中拼写我的数据库的名称值错误,因为它在我的本地环境中工作。

这是我的设置:

from pathlib import Path
import django_heroku 
import os


# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'xxxxxxxxx'

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


ALLOWED_HOSTS = ['premtablelive.herokuapp.com','127.0.0.1']


# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'table',
    'django_filters'
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'whitenoise.middleware.WhiteNoiseMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'PremTable.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': '',
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'PremTable.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2',
        'NAME': 'premtable',
        'USER': 'xxxxx',
        'PASSWORD':'xxxxxx',
        'HOST': 'database-1.casge6lscm2l.us-east-2.rds.amazonaws.com',
        'PORT':'5432',
    }
}
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age = 600)
DATABASES['default'].update(db_from_env)



# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/



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

MEDIA_URL = '/images/'

STATICFILES_DIRS = [
    os.path.join(BASE_DIR, "static"),
]

MEDIA_ROOT = os.path.join(BASE_DIR, 'static/images')



django_heroku.settings(locals())

这是我的 Procfile

web: gunicorn PremTable.wsgi --log-file -

这是我的要求

asgiref==3.3.1
boto3==1.16.35
botocore==1.19.35
dj-database-url==0.5.0
Django==3.1.3
django-filter==2.4.0
django-heroku==0.3.1
django-storages==1.10.1
freeze==3.0
gunicorn==20.0.4
jmespath==0.10.0
Pillow==8.0.1
psycopg2==2.8.6
psycopg2-binary==2.8.6
python-dateutil==2.8.1
pytz==2020.4
s3transfer==0.3.3
six==1.15.0
sqlparse==0.4.1
unicorn==1.0.2
urllib3==1.26.2
whitenoise==5.2.0

任何帮助将不胜感激!我真的无法在任何地方找到解决这个问题的方法

标签: djangodatabasepostgresqlherokudeployment

解决方案


尝试使用它,这样,您将自动将数据库 URL 配置为 heroku 提供的 URL

from dj_database_url import parse as dburl 
from decouple import config

default_dburl = "sqlite:///" + os.path.join(BASE_DIR, "db.sqlite3")


DATABASES = {"default": config("DATABASE_URL", default=default_dburl, cast=dburl)}


推荐阅读