【发布时间】:2022-01-02 12:33:06
【问题描述】:
我试图在 heroku 上托管一个网站,但我不断收到此错误
文件 "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/utils.py", 第 84 行,在 _execute 返回 self.cursor.execute(sql, params) django.db.utils.ProgrammingError: 关系“home_product”不 存在第 1 行:...home_product"."price", "home_product"."slug" FROM "home_prod...
每当我尝试使用时
heroku run python
manage.py migrate -a appname
我主要使用this video作为托管参考。
还有this StackOverflow question 和this article 也没有说明这个问题。
这是我的settings.py
"""
Django settings for Corbett_Jewelry project.
Generated by 'django-admin startproject' using Django 3.1.7.
For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/
For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""
from pathlib import Path
import os
import django_heroku
import dj_database_url
from decouple import config
import psycopg2
# 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.1/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 = os.environ.get('DJANGO_DEBUG', '') != 'False'
ALLOWED_HOSTS = ['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',
'home'
]
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',
'whitenoise.middleware.WhiteNoiseMiddleware',
]
ROOT_URLCONF = 'Corbett_Jewelry.urls'
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [os.path.join(BASE_DIR,'templates')],
'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 = 'Corbett_Jewelry.wsgi.application'
# 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_URL = '/static/'
STATICFILES_DIRS = [
os.path.join(BASE_DIR, 'static'),
]
STATIC_ROOT = os.path.join(BASE_DIR, 'static_root')
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')
DATABASE_URL = os.environ['DATABASE_URL']
conn = psycopg2.connect(DATABASE_URL, sslmode='require')
DATABASES['default'] = dj_database_url.config(conn_max_age=600, ssl_require=True)
db_from_env = dj_database_url.config(conn_max_age=500)
DATABASES['default'].update(db_from_env)
django_heroku.settings(locals())
我的models.py:
from django.db import models
class Product(models.Model):
name = models.CharField(max_length=100)
image = models.ImageField(blank=True)
desc = models.TextField()
price = models.IntegerField()
slug = models.SlugField(unique=True, default=None)
def __str__(self):
return self.name
class Images(models.Model):
product = models.ForeignKey(Product, default=None, on_delete=models.CASCADE)
img = models.ImageField(upload_to='images/')
def __str__(self):
return self.product.name
class Beads(models.Model):
name = models.CharField(max_length=200)
price = models.IntegerField()
img = models.ImageField(upload_to='beads/')
我的档案:
web: gunicorn Corbett_Jewelry.wsgi
我的requirements.txt:
asgiref==3.4.1
cycler==0.10.0
dj-database-url==0.5.0
Django==3.2.9
django-heroku==0.3.1
gunicorn==20.1.0
kiwisolver==1.3.2
matplotlib==3.4.3
numpy==1.21.2
Pillow==8.3.2
psycopg2==2.9.2
pyparsing==2.4.7
python-dateutil==2.8.2
python-decouple==3.5
pytz==2021.3
selenium==3.141.0
six==1.16.0
sqlparse==0.4.2
urllib3==1.26.7
whitenoise==5.3.0
我不知道您可能还需要什么信息来解决这个问题。我很乐意提供任何信息,因为我已经为此苦苦思考了一个星期了。
【问题讨论】:
-
我对您的尝试感到有些困惑。正如您的问题标题所暗示的那样,您似乎没有将数据从
sqlite3传输到postgres...您是否只是尝试使用postgres 而不是SQLite 实例化您的应用程序的新实例?我几乎 100% 确定您的问题在于您尝试连接到数据库的方式,因为它无法运行迁移,但我不知道dj_database_url返回什么来设置您的DATABASESattr 在设置中,你能证明一下吗? -
@ViaTech 所以我这样命名它是因为我从某个地方读到了一个有类似问题的人,他们得到的回复告诉他们这是一个转移问题。此外,我不太确定如何显示这一点,但是当我尝试打印
dj_database_url.config时,它返回了一个空字典 -
我认为如果你精简你的例子,你会获得更多的帮助。也许开始一个新问题,但请确保您有一个minimal reproducible example(并删除旧问题,以免创建重复问题)
-
@Neuron 嘿,感谢您的建议,稍后会这样做,还看到您修复了我的问题的格式,非常高兴您这样做,谢谢!
-
当然可以。很高兴我能帮上忙。当我们谈论建议时。 StackOverflow 上的人们倾向于以更专业的方式相互交流。我不会称陌生人为“兄弟”。特别是如果你甚至不知道他们的性别;)
标签: python django database postgresql heroku