[英]Python Django - Internal Error ProgrammingError relation does not exist
This might probably be a duplicated question, but I can't find a post to answer my questions yet.这可能是一个重复的问题,但我找不到帖子来回答我的问题。 Any post that is similar to this may help is appreciated.
任何与此类似的帖子都可能有所帮助,我们将不胜感激。
I tried to host my Django app using heroku.com我尝试使用 heroku.com 托管我的 Django 应用程序
git add .
git commit -m "(commit_name)"
git push heroku master
When I tried to test the website ( /questions/1
), the website shows an Error 500 (Internal Error).当我尝试测试网站 (
/questions/1
) 时,网站显示错误 500(内部错误)。 First it shows a ProgrammingError: relation does not exist.首先它显示 ProgrammingError:关系不存在。 After that I did
$ heroku run python manage.py migrate
try to solve the problem.之后我做
$ heroku run python manage.py migrate
尝试解决问题。 The original error disappeared, but instead this happened:原来的错误消失了,而是发生了:
2020-08-29T11:05:42.668070+00:00 app[web.1]: Internal Server Error: /questions/1
2020-08-29T11:05:42.668070+00:00 app[web.1]:
2020-08-29T11:05:42.668070+00:00 app[web.1]: DoesNotExist at /questions/1
2020-08-29T11:05:42.668071+00:00 app[web.1]: Question matching query does not exist.
Settings.py
: Settings.py
:
import django_heroku
from pathlib import Path
import os
#--------------------(ommited)--------------------#
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = False
ALLOWED_HOSTS = ['*']
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'myapp',
]
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 = 'myweb.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 = 'myweb.wsgi.application'
# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}
# 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 = 'zh-Hant'
TIME_ZONE = 'Asia/Taipei'
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'),
]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')
# Activate Django-Heroku.
django_heroku.settings(locals())
Models.py
: Models.py
:
from django.db import models
from django.utils import timezone
import json
# Create your models here.
class Code(models.Model):
code = models.TextField()
number = models.IntegerField()
class Question(models.Model):
title = models.TextField()
text = models.TextField()
judges = models.TextField()
number = models.IntegerField()
class Meta:
ordering = ['number']
def set_judges(self, x):
self.judges = json.dumps(x)
def get_judges(self):
return json.loads(self.judges)
wsgi.py
: wsgi.py
:
import os
from dj_static import Cling
from django.core.wsgi import get_wsgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myweb.settings')
application = Cling(get_wsgi_application())
Any suggestions or things I should try?有什么建议或我应该尝试的事情吗? Thank you.
谢谢你。
[EDIT]: It seems that the database is empty now so it cause the error. [编辑]:似乎数据库现在是空的,所以它导致了错误。 But when I run the same file in my computer as I git push to heroku, the database isn't empty and it works fine.
但是当我在我的计算机上运行相同的文件时,我将 git 推送到 heroku,数据库不为空并且工作正常。
According to your settings file, you are using sqlite as the database, and you can't use it in Heroku.根据你的设置文件,你使用的是sqlite作为数据库,Heroku是不能用的。
Heroku uses an an ephemeral filesystem . Heroku 使用一个临时文件系统。
You can write to it, and you can read from it, but the contents will be cleared periodically.
您可以写入,也可以从中读取,但内容会定期清除。 If you were to use SQLite on Heroku, you would lose your entire database at least once every 24 hours.
如果你在 Heroku 上使用 SQLite,你将至少每 24 小时丢失一次整个数据库。
That's why it works locally but not on Heroku, you need to use another database engine like postgresql for example.这就是为什么它在本地工作而不在 Heroku 上工作的原因,您需要使用另一个数据库引擎,例如postgresql 。
Learn more about it at: https://help.heroku.com/K1PPS2WM/why-are-my-file-uploads-missing-deleted在以下位置了解更多信息: https://help.heroku.com/K1PPS2WM/why-are-my-file-uploads-missing-deleted
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.