我似乎找不到从我的 django 应用程序写入 heroku 的 postgres 的方法
I can't seem to find a way to write into heroku's postgres from my django app
我用 Django 编写了一个应用程序,在本地 运行ning 时它一直运行良好。我知道用 Heroku 和应用程序 运行 也部署了它。
但是,当我 python manage.py createsuperuser
它说我成功创建了超级用户,但没有任何内容写入 Heroku Postgress DB...所以我既不能登录 /admin
也不能登录我的应用程序。
我一直在疯狂地调试,但我找不到问题所在。特别是因为如果我从 django Shell 查询数据库,我会看到我的帐户,但是如果我检查 Heroku 的 Postgress 数据库,我不会。
我还检查了我的 python shell 连接到哪个数据库,它似乎是 sqlite,而不是 postgres。
- 如何更改它以便它连接到 postgres?
- 此外,为什么它连接到我的 shell 上的 Sqlite,但如果我尝试登录管理,它会检查我的用户是否存在于 postgres 上?
这是我的settings.py
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ...
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG=True
#DEBUG_PROPAGATE_EXCEPTIONS = True
ALLOWED_HOSTS = ['localhost', '127.0.0.1', 'produceit.herokuapp.com']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'action.apps.ActionConfig',
'django_extensions',
'bootstrap_modal_forms',
'widget_tweaks',
]
MIDDLEWARE = [
'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 = 'mywebsite.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 = 'mywebsite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Parse database configuration from $DATABASE_URL
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
# Password validation
# https://docs.djangoproject.com/en/3.2/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.2/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.2/howto/static-files/
# Static files (CSS, JavaScript, Images)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
#DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
#import django_on_heroku
#django_on_heroku.settings(locals())
import django_heroku
django_heroku.settings(locals())
好的,所以当我尝试将我的网站上传到 Heroku 时,我遇到过很多次这个问题。上次我做这样的事情时它对我有用:
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': '...',
'HOST': '...',
'PORT': 5432,
'USER': '...',
'PASSWORD': '...',
名称、主机、用户和密码都可以在您在 Heroku 本身中创建 postresql 插件时找到。您可能已经默认添加了 postres。如果没有,您可以自己添加。你可能想搜索 YouTube 视频,因为我都是从那里找到的。我不记得是哪一个,但如果记得,我会 link 放在这里。
当您在 Heroku 中转到您的应用程序时,转到设置并单击显示配置变量。复制数据库的 link 并将其粘贴到某处,以便您可以将其用作参考。会是这样的。
postgres://USER:PASSWORD@HOST:PORT/NAME
听起来您可能是在本地实例上而不是在 Heroku 上的版本上创建超级用户。尝试在您的 Heroku 托管站点上创建一个 Django 超级用户。
heroku 运行 python manage.py createsuperuser
我用 Django 编写了一个应用程序,在本地 运行ning 时它一直运行良好。我知道用 Heroku 和应用程序 运行 也部署了它。
但是,当我 python manage.py createsuperuser
它说我成功创建了超级用户,但没有任何内容写入 Heroku Postgress DB...所以我既不能登录 /admin
也不能登录我的应用程序。
我一直在疯狂地调试,但我找不到问题所在。特别是因为如果我从 django Shell 查询数据库,我会看到我的帐户,但是如果我检查 Heroku 的 Postgress 数据库,我不会。
我还检查了我的 python shell 连接到哪个数据库,它似乎是 sqlite,而不是 postgres。
- 如何更改它以便它连接到 postgres?
- 此外,为什么它连接到我的 shell 上的 Sqlite,但如果我尝试登录管理,它会检查我的用户是否存在于 postgres 上?
这是我的settings.py
from pathlib import Path
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.2/howto/deployment/checklist/
# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = ...
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG=True
#DEBUG_PROPAGATE_EXCEPTIONS = True
ALLOWED_HOSTS = ['localhost', '127.0.0.1', 'produceit.herokuapp.com']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'action.apps.ActionConfig',
'django_extensions',
'bootstrap_modal_forms',
'widget_tweaks',
]
MIDDLEWARE = [
'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 = 'mywebsite.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 = 'mywebsite.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.2/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# Parse database configuration from $DATABASE_URL
import dj_database_url
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
# Password validation
# https://docs.djangoproject.com/en/3.2/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.2/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.2/howto/static-files/
# Static files (CSS, JavaScript, Images)
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
# Default primary key field type
# https://docs.djangoproject.com/en/3.2/ref/settings/#default-auto-field
#DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
#import django_on_heroku
#django_on_heroku.settings(locals())
import django_heroku
django_heroku.settings(locals())
好的,所以当我尝试将我的网站上传到 Heroku 时,我遇到过很多次这个问题。上次我做这样的事情时它对我有用:
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': '...',
'HOST': '...',
'PORT': 5432,
'USER': '...',
'PASSWORD': '...',
名称、主机、用户和密码都可以在您在 Heroku 本身中创建 postresql 插件时找到。您可能已经默认添加了 postres。如果没有,您可以自己添加。你可能想搜索 YouTube 视频,因为我都是从那里找到的。我不记得是哪一个,但如果记得,我会 link 放在这里。
当您在 Heroku 中转到您的应用程序时,转到设置并单击显示配置变量。复制数据库的 link 并将其粘贴到某处,以便您可以将其用作参考。会是这样的。
postgres://USER:PASSWORD@HOST:PORT/NAME
听起来您可能是在本地实例上而不是在 Heroku 上的版本上创建超级用户。尝试在您的 Heroku 托管站点上创建一个 Django 超级用户。
heroku 运行 python manage.py createsuperuser