Django APP部署到Heroku的问题

Posted

技术标签:

【中文标题】Django APP部署到Heroku的问题【英文标题】:Problem with Django APP deployment to Heroku 【发布时间】:2022-01-22 10:47:17 【问题描述】:

我有一个问题,我找不到解决方案。

我已经尝试使用mysqlpostgresql 部署到Heroku,但我总是遇到某种错误。

该应用在本地环境中运行良好。

日志:

Cannot execute silk_profile as silk is not installed correctly.

Traceback (most recent call last):

  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 230, in ensure_connection

    self.connect()

  File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner

    return func(*args, **kwargs)

  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 211, in connect

    self.connection = self.get_new_connection(conn_params)

  File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner

    return func(*args, **kwargs)

  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 199, in get_new_connection

    connection = Database.connect(**conn_params)

  File "/app/.heroku/python/lib/python3.9/site-packages/psycopg2/__init__.py", line 122, in connect

    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)

psycopg2.OperationalError: connection to server at "127.0.0.1", port 5432 failed: Connection refused

    Is the server running on that host and accepting TCP/IP connections?

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

Traceback (most recent call last):

  File "/app/manage.py", line 22, in <module>

    main()

  File "/app/manage.py", line 18, in main

    execute_from_command_line(sys.argv)

  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 425, in execute_from_command_line

    utility.execute()

  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 419, in execute

    self.fetch_command(subcommand).run_from_argv(self.argv)

  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 373, in run_from_argv

    self.execute(*args, **cmd_options)

  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 417, in execute

    output = self.handle(*args, **options)

  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 90, in wrapped

    res = handle_func(*args, **kwargs)

  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/commands/migrate.py", line 75, in handle

    self.check(databases=[database])

  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/base.py", line 438, in check

    all_issues = checks.run_checks(

  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/checks/registry.py", line 77, in run_checks

    new_errors = check(app_configs=app_configs, databases=databases)

  File "/app/.heroku/python/lib/python3.9/site-packages/django/core/checks/model_checks.py", line 34, in check_all_models

    errors.extend(model.check(**kwargs))

  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/base.py", line 1307, in check

    *cls._check_indexes(databases),

  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/base.py", line 1699, in _check_indexes

    connection.features.supports_covering_indexes or

  File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__

    res = instance.__dict__[self.name] = self.func(instance)

  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/postgresql/features.py", line 84, in is_postgresql_11

    return self.connection.pg_version >= 110000

  File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/functional.py", line 48, in __get__

    res = instance.__dict__[self.name] = self.func(instance)

  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 339, in pg_version

    with self.temporary_connection():

  File "/app/.heroku/python/lib/python3.9/contextlib.py", line 119, in __enter__

    return next(self.gen)

  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 614, in temporary_connection

    with self.cursor() as cursor:

  File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner

    return func(*args, **kwargs)

  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 270, in cursor

    return self._cursor()

  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 246, in _cursor

    self.ensure_connection()

  File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner

    return func(*args, **kwargs)

  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 230, in ensure_connection

    self.connect()

  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/utils.py", line 90, in __exit__

    raise dj_exc_value.with_traceback(traceback) from exc_value

  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 230, in ensure_connection

    self.connect()

  File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner

    return func(*args, **kwargs)

  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/base/base.py", line 211, in connect

    self.connection = self.get_new_connection(conn_params)

  File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/asyncio.py", line 25, in inner

    return func(*args, **kwargs)

  File "/app/.heroku/python/lib/python3.9/site-packages/django/db/backends/postgresql/base.py", line 199, in get_new_connection

    connection = Database.connect(**conn_params)

  File "/app/.heroku/python/lib/python3.9/site-packages/psycopg2/__init__.py", line 122, in connect

    conn = _connect(dsn, connection_factory=connection_factory, **kwasync)

django.db.utils.OperationalError: connection to server at "127.0.0.1", port 5432 failed: Connection refused

    Is the server running on that host and accepting TCP/IP connections? 

Psql 在本地运行,silk 已安装。当我尝试使用mysql 时,我也遇到了连接问题。

我每次都按照每一步,但我找不到问题所在。

【问题讨论】:

【参考方案1】:

我认为您无法将部署在 Heroku 上的 Django 应用程序与本地数据库连接,但您必须使用具有特定服务的公共数据库,例如您可以使用 AWS RDS。

【讨论】:

它没有部署在本地数据库中,而是部署在 Heroku Postgresql 中。这就是为什么我不明白这个错误。我已经用 DATABASE_URL 连接了 heroku 中的 de DB。 heroku 配置:设置 DATABASE_URL=postgres://.......

以上是关于Django APP部署到Heroku的问题的主要内容,如果未能解决你的问题,请参考以下文章

Heroku 部署与 django 数据库问题

Django [Mezzanine CMS] 项目未部署到 Heroku

Django React -- Heroku 上的部署错误(找不到页面 404)

当我将django应用程序部署到heroku时,该站点停止工作

在Heroku上部署Django应用程序:错误[R10]

Django 'pip install django-heroku'(psycopg2) 错误阻止部署到 Heroku