【问题标题】:SQLite Database not connected in Django HerokuSQLite 数据库未在 Django Heroku 中连接
【发布时间】:2020-12-04 02:02:30
【问题描述】:

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

"""
Django settings for dss_project project.

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

For more information 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)

【问题讨论】:

    标签: django heroku web-deployment


    【解决方案1】:

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

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

    【讨论】:

    • 好吧实际上没有理由,因为据我所知,django 的默认数据库是 sqlite。我之前用 sqlite 部署过其他项目,没有问题
    • Heroku 在其服务器上托管另一个数据库实例,这就是它为空的原因。尝试运行“heroku run python manage.py createsuperuser”在生产数据库中创建超级用户并尝试登录。
    • 我按照你说的做了,好像表格还在,但是所有的记录都是空的,我不明白,我想当我推后运行heroku run python manage.py migrate项目它将迁移它们。还有其他方法可以从本地迁移数据库吗?
    • 原来我忘记了我很久以前就已经选择了 postgresql,甚至在我尝试在 heroku 中使用相同的应用程序名称进行部署时,我什至推送了我的项目。这解释了我的第一条评论的答案。我想我只需要像你说的那样改为postgresql,因为它更合适。
    • migrate 命令将数据库与您的模型和以前的迁移同步。它不会使任何项目“出现”在任何地方。您在 Heroku 上的数据库无法访问存储在本地 db.sqlite3 文件中的记录,这就是您在那里看不到它们的原因。基本上,您正在处理两个不同的数据库,一个用于本地开发,另一个连接到托管您网站的任何服务器(在本例中为 Heroku)。
    猜你喜欢
    • 2022-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2021-10-27
    • 2017-09-13
    相关资源
    最近更新 更多