尝试迁移数据库时,Django应用在Heroku上崩溃

时间:2019-04-23 10:37:19

标签: python django python-3.x heroku heroku-postgres

我提交凭据后尝试登录时,已经部署的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

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

2 个答案:

答案 0 :(得分:3)

您正在尝试连接到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())
    

答案 1 :(得分:0)

似乎其中一个模板有错误,您在其中尝试使用模板标签static而不导入它。