我的应用程序可以在我的本地计算机上运行,但是当使用完全相同的代码部署到 heroku 时,我在对涉及任何数据库交互的 API 视图使用 get 请求时遇到错误。我有一种感觉 heroku 出于某种原因试图使用 postgres,即使我的代码中没有任何处理 postgres 的代码。任何帮助表示赞赏,我已经完成了一百万种不同的迁移方式,但没有运气,即使 mongo 不需要迁移。
错误和代码如下
2019-11-21T18:26:15.428986+00:00 app[web.1]: Traceback (most recent call last):
2019-11-21T18:26:15.428989+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/exception.py", line 34, in inner
2019-11-21T18:26:15.428991+00:00 app[web.1]: response = get_response(request)
2019-11-21T18:26:15.428993+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py", line 115, in _get_response
2019-11-21T18:26:15.428995+00:00 app[web.1]: response = self.process_exception_by_middleware(e, request)
2019-11-21T18:26:15.428997+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/core/handlers/base.py", line 113, in _get_response
2019-11-21T18:26:15.429000+00:00 app[web.1]: response = wrapped_callback(request, *callback_args, **callback_kwargs)
2019-11-21T18:26:15.429002+00:00 app[web.1]: File "/app/calculator/views.py", line 22, in calculate
2019-11-21T18:26:15.429004+00:00 app[web.1]: print(calcs)
2019-11-21T18:26:15.429006+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 250, in __repr__
2019-11-21T18:26:15.429008+00:00 app[web.1]: data = list(self[:REPR_OUTPUT_SIZE + 1])
2019-11-21T18:26:15.429010+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 274, in __iter__
2019-11-21T18:26:15.429012+00:00 app[web.1]: self._fetch_all()
2019-11-21T18:26:15.429014+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 1242, in _fetch_all
2019-11-21T18:26:15.429016+00:00 app[web.1]: self._result_cache = list(self._iterable_class(self))
2019-11-21T18:26:15.429018+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/query.py", line 55, in __iter__
2019-11-21T18:26:15.429020+00:00 app[web.1]: results = compiler.execute_sql(chunked_fetch=self.chunked_fetch, chunk_size=self.chunk_size)
2019-11-21T18:26:15.429022+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/models/sql/compiler.py", line 1100, in execute_sql
2019-11-21T18:26:15.429024+00:00 app[web.1]: cursor.execute(sql, params)
2019-11-21T18:26:15.429026+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute
2019-11-21T18:26:15.429028+00:00 app[web.1]: return super().execute(sql, params)
2019-11-21T18:26:15.429030+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
2019-11-21T18:26:15.429032+00:00 app[web.1]: return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
2019-11-21T18:26:15.429034+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
2019-11-21T18:26:15.429036+00:00 app[web.1]: return executor(sql, params, many, context)
2019-11-21T18:26:15.429038+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
2019-11-21T18:26:15.429040+00:00 app[web.1]: return self.cursor.execute(sql, params)
2019-11-21T18:26:15.429042+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
2019-11-21T18:26:15.429044+00:00 app[web.1]: raise dj_exc_value.with_traceback(traceback) from exc_value
2019-11-21T18:26:15.429046+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
2019-11-21T18:26:15.429048+00:00 app[web.1]: return self.cursor.execute(sql, params)
2019-11-21T18:26:15.429050+00:00 app[web.1]: django.db.utils.ProgrammingError: column calculator_calculation._id does not exist
2019-11-21T18:26:15.429052+00:00 app[web.1]: LINE 1: SELECT "calculator_calculation"."_id", "calculator_calculati...
2019-11-21T18:26:15.429054+00:00 app[web.1]: ^
2019-11-21T18:26:15.429062+00:00 app[web.1]:
据我所知,我的 mongoDB 已正确导入所有内容并且设置正确
import os
import environ
import django_heroku
env = os.environ
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
SECRET_KEY = 'secret'
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
ALLOWED_HOSTS = []
STATICFILES_DIRS = (
)
# STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
# Application definition
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'calculator.apps.CalculatorConfig'
]
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 = 'sezzleCalculator.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 = 'sezzleCalculator.wsgi.application'
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
DATABASES = {
"default": {
"ENGINE": "djongo",
"NAME": "appDB",
"HOST": "hostURL",
"PASSWORD": "password",
"USER": "randomuser",
"AUTH_MECHANISM": 'SCRAM-SHA-1',
"AUTH_SOURCE": "admin",
"ENFORCE_SCHEMA": False,
}
}
# Password validation
# https://docs.djangoproject.com/en/2.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/2.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/2.2/howto/static-files/
STATIC_URL = '/static/'
django_heroku.settings(locals())
它在此获取请求上出错。
def calculate(request):
if request.method == "GET":
calcs = Calculation.objects.filter().order_by("_id")
print(calcs)
calcsReversed = calcs.reverse()
response = {"status": "1", "message": ("OK")}
idx = 0
for calc in calcsReversed:
if idx > 9:
calc.delete()
response[idx] = {"first":calc.first,"second":calc.second,"operator":calc.operator,"result":calc.result}
idx +=1
print(response)
return HttpResponse(json.dumps(response), content_type="application/json")
elif request.method == "POST":
jsonIncoming = json.loads(request.body.decode("utf8"))
print(jsonIncoming)
calculation = Calculation(result=jsonIncoming["result"], first=jsonIncoming["first"],second=jsonIncoming["second"],operator=jsonIncoming["operator"])
calculation.save()
response = {"status": "1", "message": ("OK"), "type":"post"}
return HttpResponse(json.dumps(response), content_type="application/json")