Сбой приложения Django на Heroku при попытке перенести базу данных

#python #django #python-3.x #heroku #heroku-postgres

#python #django #python-3.x #heroku #heroku-postgres

Вопрос:

Развернутое мной веб-приложение выходит из строя при запуске, как только я пытаюсь войти в систему после отправки учетных данных.

Обычно мне приходится запускать:

 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
  

Что является причиной этого и как я могу это исправить?

Ответ №1:

Вы пытаетесь подключиться к базе данных на 127.0.0.1 , но служба Postgres Heroku не работает таким образом. Ваша база данных не будет находиться на том же компьютере, что и код вашего приложения.

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())
      

Ответ №2:

Похоже, у вас ошибка в одном из ваших шаблонов, где вы пытаетесь использовать тег шаблона static без его импорта.