在Django Heroku中未连接SQLite数据库

问题描述

因此,我试图将我的项目部署到Heroku,我遵循了以下步骤:https://www.codementor.io/@jamesezechukwu/how-to-deploy-django-app-on-heroku-dtsee04d4 。构建成功,我能够运行服务器:https://pcbuildingparts.herokuapp.com/ 但是数据库似乎无法连接,因为数据库中的项目根本没有显示,因此我尝试在部署之前使用超级用户帐户登录管理站点,但它不允许我进入。 这是我的settings.py

"""
Django settings for dss_project project.

Generated by 'django-admin startproject' using Django 3.0.3.

For more @R_395_4045@ion on this file,see
https://docs.djangoproject.com/en/3.0/topics/settings/

For the full list of settings and their values,see
https://docs.djangoproject.com/en/3.0/ref/settings/
"""

import os

# Build paths inside the project like this: os.path.join(BASE_DIR,...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
TEMPLATE_DIR = os.path.join(BASE_DIR,"templates")



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

# Security WARNING: keep the secret key used in production secret!
SECRET_KEY = '5u53+p4k-_=70wwg-igg7_5r9!5vh-c6@@hcl&**(e6fod8d(a'

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

ALLOWED_HOSTS = ['pcbuildingparts.herokuapp.com']


# Application deFinition

INSTALLED_APPS = [
    'django.contrib.admin','django.contrib.auth','django.contrib.contenttypes','django.contrib.sessions','django.contrib.messages','django.contrib.staticfiles','bootstrap4','blog','accounts','sim','dss',]

MIDDLEWARE = [
    'whitenoise.middleware.WhiteNoiseMiddleware','django.middleware.security.SecurityMiddleware','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 = 'dss_project.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates','Dirs': [TEMPLATE_DIR,],'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 = 'dss_project.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.0/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3','NAME': os.path.join(BASE_DIR,'db.sqlite3'),}
}


# Password validation
# https://docs.djangoproject.com/en/3.0/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.0/topics/i18n/

LANGUAGE_CODE = 'id'

TIME_ZONE = 'UTC'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS,JavaScript,Images)
# https://docs.djangoproject.com/en/3.0/howto/static-files/
PROJECT_ROOT   =   os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
STATIC_ROOT  =   os.path.join(PROJECT_ROOT,'staticfiles')
STATIC_URL = '/static/'
STATICFILES_Dirs = [os.path.join(PROJECT_ROOT,'static')]

MEDIA_ROOT = os.path.join(BASE_DIR,'media')
MEDIA_URL = '/media/'

STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesstorage'

LOGIN_REDIRECT_URL = "/"
logoUT_REDIRECT_URL = "/"

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

解决方法

您为什么要在Heroku上使用SQLite是否有特定原因?一个不错的选择是使用postgress addon并使用SQLite进行本地开发和测试。

查看如何在Django中连接到Postgres:postgres 您还可以使用django-heroku软件包轻松配置django应用。