gpt4 book ai didi

python - 尝试迁移数据库时 Django 应用程序在 Heroku 上崩溃

转载 作者:太空宇宙 更新时间:2023-11-04 11:21:52 25 4
gpt4 key购买 nike

我在提交凭据后尝试登录时,我部署的 Web 应用程序在生产环境中立即崩溃。

通常,我必须运行:

heroku run python manage.py migrate 

更新 Heroku 数据库中的更改,但是当我这样做时,请在下面找到发生了什么。

这是终端返回的内容。

Running python manage.py migrate on ⬢ friendsbook-demo... up, run.1779 (Hobby)
Traceback (most recent call last):
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
self.connect()
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/backends/base/base.py", line 194, in connect
self.connection = self.get_new_connection(conn_params)
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/backends/postgresql/base.py", line 168, in get_new_connection
connection = Database.connect(**conn_params)
File "/app/.heroku/python/lib/python3.7/site-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
psycopg2.OperationalError: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
File "manage.py", line 15, in <module>
execute_from_command_line(sys.argv)
File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
utility.execute()
File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/__init__.py", line 365, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
self.execute(*args, **cmd_options)
File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/base.py", line 335, in execute
output = self.handle(*args, **options)
File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/commands/migrate.py", line 79, in handle
executor = MigrationExecutor(connection, self.migration_progress_callback)
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/migrations/executor.py", line 18, in __init__
self.loader = MigrationLoader(self.connection)
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/migrations/loader.py", line 49, in __init__
self.build_graph()
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/migrations/loader.py", line 206, in build_graph
self.applied_migrations = recorder.applied_migrations()
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/migrations/recorder.py", line 61, in applied_migrations
if self.has_table():
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/migrations/recorder.py", line 44, in has_table
return self.Migration._meta.db_table in self.connection.introspection.table_names(self.connection.cursor())
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/backends/base/base.py", line 255, in cursor
return self._cursor()
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/backends/base/base.py", line 232, in _cursor
self.ensure_connection()
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
self.connect()
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/utils.py", line 89, in __exit__
raise dj_exc_value.with_traceback(traceback) from exc_value
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/backends/base/base.py", line 216, in ensure_connection
self.connect()
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/backends/base/base.py", line 194, in connect
self.connection = self.get_new_connection(conn_params)
File "/app/.heroku/python/lib/python3.7/site-packages/django/db/backends/postgresql/base.py", line 168, in get_new_connection
connection = Database.connect(**conn_params)
File "/app/.heroku/python/lib/python3.7/site-packages/psycopg2/__init__.py", line 130, in connect
conn = _connect(dsn, connection_factory=connection_factory, **kwasync)
django.db.utils.OperationalError: could not connect to server: Connection refused
Is the server running on host "127.0.0.1" and accepting
TCP/IP connections on port 5432?

我在 Stack Overflow 上看到了一些关于检查 Postgres 是否正在运行的代码,并尝试了其中一些,但它们不起作用。

这是我运行 heroku logs -t 时发生的情况:

2019-04-23T02:27:22.203770+00:00 app[web.1]: Traceback (most recent call last):
2019-04-23T02:27:22.203772+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/base.py", line 475, in parse
2019-04-23T02:27:22.203773+00:00 app[web.1]: compile_func = self.tags[command]
2019-04-23T02:27:22.203775+00:00 app[web.1]: KeyError: 'static'
2019-04-23T02:27:22.203776+00:00 app[web.1]:
2019-04-23T02:27:22.203778+00:00 app[web.1]: During handling of the above exception, another exception occurred:
2019-04-23T02:27:22.203779+00:00 app[web.1]:
2019-04-23T02:27:22.203781+00:00 app[web.1]: Traceback (most recent call last):
2019-04-23T02:27:22.203782+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/exception.py", line 35, in inner
2019-04-23T02:27:22.203784+00:00 app[web.1]: response = get_response(request)
2019-04-23T02:27:22.203785+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/whitenoise/middleware.py", line 49, in __call__
2019-04-23T02:27:22.203787+00:00 app[web.1]: response = self.get_response(request)
2019-04-23T02:27:22.203789+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/exception.py", line 37, in inner
2019-04-23T02:27:22.203790+00:00 app[web.1]: response = response_for_exception(request, exc)
2019-04-23T02:27:22.203792+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/exception.py", line 87, in response_for_exception
2019-04-23T02:27:22.203794+00:00 app[web.1]: response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
2019-04-23T02:27:22.203796+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/exception.py", line 126, in handle_uncaught_exception
2019-04-23T02:27:22.203797+00:00 app[web.1]: return callback(request, **param_dict)
2019-04-23T02:27:22.203798+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
2019-04-23T02:27:22.203800+00:00 app[web.1]: response = view_func(request, *args, **kwargs)
2019-04-23T02:27:22.203801+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/views/defaults.py", line 69, in server_error
2019-04-23T02:27:22.203803+00:00 app[web.1]: template = loader.get_template(template_name)
2019-04-23T02:27:22.203812+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/loader.py", line 15, in get_template
2019-04-23T02:27:22.203814+00:00 app[web.1]: return engine.get_template(template_name)
2019-04-23T02:27:22.203816+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/backends/django.py", line 34, in get_template
2019-04-23T02:27:22.203817+00:00 app[web.1]: return Template(self.engine.get_template(template_name), self)
2019-04-23T02:27:22.203819+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/engine.py", line 144, in get_template
2019-04-23T02:27:22.203821+00:00 app[web.1]: template, origin = self.find_template(template_name)
2019-04-23T02:27:22.203822+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/engine.py", line 126, in find_template
2019-04-23T02:27:22.203824+00:00 app[web.1]: template = loader.get_template(name, skip=skip)
2019-04-23T02:27:22.203825+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/loaders/cached.py", line 55, in get_template
2019-04-23T02:27:22.203827+00:00 app[web.1]: template = super().get_template(template_name, skip)
2019-04-23T02:27:22.203829+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/loaders/base.py", line 30, in get_template
2019-04-23T02:27:22.203832+00:00 app[web.1]: contents, origin, origin.template_name, self.engine,
2019-04-23T02:27:22.203833+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/base.py", line 160, in __init__
2019-04-23T02:27:22.203834+00:00 app[web.1]: self.nodelist = self.compile_nodelist()
2019-04-23T02:27:22.203835+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/base.py", line 198, in compile_nodelist
2019-04-23T02:27:22.203836+00:00 app[web.1]: return parser.parse()
2019-04-23T02:27:22.203837+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/base.py", line 477, in parse
2019-04-23T02:27:22.203839+00:00 app[web.1]: self.invalid_block_tag(token, command, parse_until)
2019-04-23T02:27:22.203840+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/base.py", line 539, in invalid_block_tag
2019-04-23T02:27:22.203841+00:00 app[web.1]: "or load this tag?" % (token.lineno, command)
2019-04-23T02:27:22.203842+00:00 app[web.1]: django.template.exceptions.TemplateSyntaxError: Invalid block tag on line 10: 'static'. Did you forget to register or load this tag?
2019-04-23T02:27:22.203844+00:00 app[web.1]:
2019-04-23T02:27:22.203845+00:00 app[web.1]: During handling of the above exception, another exception occurred:
2019-04-23T02:27:22.203846+00:00 app[web.1]:
2019-04-23T02:27:22.203847+00:00 app[web.1]: Traceback (most recent call last):
2019-04-23T02:27:22.203848+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/base.py", line 475, in parse
2019-04-23T02:27:22.203849+00:00 app[web.1]: compile_func = self.tags[command]
2019-04-23T02:27:22.203850+00:00 app[web.1]: KeyError: 'static'
2019-04-23T02:27:22.203851+00:00 app[web.1]:
2019-04-23T02:27:22.203852+00:00 app[web.1]: During handling of the above exception, another exception occurred:
2019-04-23T02:27:22.203853+00:00 app[web.1]:
2019-04-23T02:27:22.203854+00:00 app[web.1]: Traceback (most recent call last):
2019-04-23T02:27:22.203855+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/workers/sync.py", line 135, in handle
2019-04-23T02:27:22.203857+00:00 app[web.1]: self.handle_request(listener, req, client, addr)
2019-04-23T02:27:22.203858+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/gunicorn/workers/sync.py", line 176, in handle_request
2019-04-23T02:27:22.203864+00:00 app[web.1]: respiter = self.wsgi(environ, resp.start_response)
2019-04-23T02:27:22.203865+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/wsgi.py", line 146, in __call__
2019-04-23T02:27:22.203866+00:00 app[web.1]: response = self.get_response(request)
2019-04-23T02:27:22.203867+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/base.py", line 81, in get_response
2019-04-23T02:27:22.203868+00:00 app[web.1]: response = self._middleware_chain(request)
2019-04-23T02:27:22.203869+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/exception.py", line 37, in inner
2019-04-23T02:27:22.203870+00:00 app[web.1]: response = response_for_exception(request, exc)
2019-04-23T02:27:22.203875+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/exception.py", line 87, in response_for_exception
2019-04-23T02:27:22.203876+00:00 app[web.1]: response = handle_uncaught_exception(request, get_resolver(get_urlconf()), sys.exc_info())
2019-04-23T02:27:22.203877+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/core/handlers/exception.py", line 126, in handle_uncaught_exception
2019-04-23T02:27:22.203878+00:00 app[web.1]: return callback(request, **param_dict)
2019-04-23T02:27:22.203879+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/utils/decorators.py", line 142, in _wrapped_view
2019-04-23T02:27:22.203880+00:00 app[web.1]: response = view_func(request, *args, **kwargs)
2019-04-23T02:27:22.203881+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/views/defaults.py", line 69, in server_error
2019-04-23T02:27:22.203882+00:00 app[web.1]: template = loader.get_template(template_name)
2019-04-23T02:27:22.203883+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/loader.py", line 15, in get_template
2019-04-23T02:27:22.203884+00:00 app[web.1]: return engine.get_template(template_name)
2019-04-23T02:27:22.203886+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/backends/django.py", line 34, in get_template
2019-04-23T02:27:22.203887+00:00 app[web.1]: return Template(self.engine.get_template(template_name), self)
2019-04-23T02:27:22.203888+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/engine.py", line 144, in get_template
2019-04-23T02:27:22.203889+00:00 app[web.1]: template, origin = self.find_template(template_name)
2019-04-23T02:27:22.203890+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/engine.py", line 126, in find_template
2019-04-23T02:27:22.203892+00:00 app[web.1]: template = loader.get_template(name, skip=skip)
2019-04-23T02:27:22.203893+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/loaders/cached.py", line 55, in get_template
2019-04-23T02:27:22.203894+00:00 app[web.1]: template = super().get_template(template_name, skip)
2019-04-23T02:27:22.203896+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/loaders/base.py", line 30, in get_template
2019-04-23T02:27:22.203897+00:00 app[web.1]: contents, origin, origin.template_name, self.engine,
2019-04-23T02:27:22.203898+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/base.py", line 160, in __init__
2019-04-23T02:27:22.203899+00:00 app[web.1]: self.nodelist = self.compile_nodelist()
2019-04-23T02:27:22.203900+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/base.py", line 198, in compile_nodelist
2019-04-23T02:27:22.203901+00:00 app[web.1]: return parser.parse()
2019-04-23T02:27:22.203902+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/base.py", line 477, in parse
2019-04-23T02:27:22.203903+00:00 app[web.1]: self.invalid_block_tag(token, command, parse_until)
2019-04-23T02:27:22.203905+00:00 app[web.1]: File "/app/.heroku/python/lib/python3.7/site-packages/django/template/base.py", line 539, in invalid_block_tag
2019-04-23T02:27:22.203906+00:00 app[web.1]: "or load this tag?" % (token.lineno, command)
2019-04-23T02:27:22.203907+00:00 app[web.1]: django.template.exceptions.TemplateSyntaxError: Invalid block tag on line 10: 'static'. Did you forget to register or load this tag?
2019-04-23T02:27:22.203908+00:00 app[web.1]: 10.65.34.10 - - [22/Apr/2019:22:27:22 -0400] "POST /login/ HTTP/1.1" 500 0 "-" "-"
2019-04-23T02:33:48.861658+00:00 app[api]: Starting process with command `python manage.py migrate` by user accelcapital18@gmail.com
2019-04-23T02:33:54.228660+00:00 heroku[run.1779]: Awaiting client
2019-04-23T02:33:54.274166+00:00 heroku[run.1779]: Starting process with command `python manage.py migrate`
2019-04-23T02:33:54.532907+00:00 heroku[run.1779]: State changed from starting to up
2019-04-23T02:33:58.977205+00:00 heroku[run.1779]: State changed from up to complete
2019-04-23T02:33:58.954246+00:00 heroku[run.1779]: Process exited with status 1

这是什么原因造成的,我该如何解决?

最佳答案

您正在尝试连接到 127.0.0.1 上的数据库,但 Heroku 的 Postgres 服务不会以这种方式运行。您的数据库不会与您的应用程序代码位于同一台机器上。

Heroku 将通过 DATABASE_URL 环境变量提供您的数据库凭据。有几种不同的方法可以使用它,但这里有一些流行的方法:

  • 使用 dj-database-url ,例如通过将它添加到您的应用程序依赖项,然后在您的 settings.py 中执行类似的操作:

    import dj_database_url

    DATABASES['default'] = dj_database_url.config(default='postgres://...')

    这告诉 Django 使用 DATABASE_URL 中的值(如果存在),否则返回到提供的 default 值,例如在你的开发机器上。

  • 使用 django-heroku ,由 Heroku 维护,它负责处理您的 DATABASE_URL 和 Heroku 推荐的其他一些事情。对于这个,将其添加为依赖项,然后将其添加到 settings.py 的底部:

    import django_heroku
    django_heroku.settings(locals())

关于python - 尝试迁移数据库时 Django 应用程序在 Heroku 上崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55809372/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com