Django/Heroku 第一次部署导致 H10 错误

Django/Heroku first deployment results in H10 error

当我在部署后尝试访问我的应用程序时,它导致 H10 错误。它似乎在日志中引用了 favicon,还说 Django 没有安装(我的 venv 中有 运行 pip3 list,它似乎在那里,它也在 requirements.txt).

这是日志:

2022-01-14T04:51:29.245939+00:00 heroku[web.1]: Starting process with command `python manage.py runserver 0.0.0.0:42836`
2022-01-14T04:51:30.238629+00:00 app[web.1]: Traceback (most recent call last):
2022-01-14T04:51:30.238658+00:00 app[web.1]: File "manage.py", line 11, in main
2022-01-14T04:51:30.238659+00:00 app[web.1]: from django.core.management import execute_from_command_line
2022-01-14T04:51:30.238659+00:00 app[web.1]: ModuleNotFoundError: No module named 'django'
2022-01-14T04:51:30.238660+00:00 app[web.1]: 
2022-01-14T04:51:30.238660+00:00 app[web.1]: The above exception was the direct cause of the following exception:
2022-01-14T04:51:30.238660+00:00 app[web.1]: 
2022-01-14T04:51:30.238660+00:00 app[web.1]: Traceback (most recent call last):
2022-01-14T04:51:30.238661+00:00 app[web.1]: File "manage.py", line 22, in <module>
2022-01-14T04:51:30.238661+00:00 app[web.1]: main()
2022-01-14T04:51:30.238661+00:00 app[web.1]: File "manage.py", line 13, in main
2022-01-14T04:51:30.238662+00:00 app[web.1]: raise ImportError(
2022-01-14T04:51:30.238672+00:00 app[web.1]: ImportError: Couldn't import Django. Are you sure it's installed and available on your PYTHONPATH environment variable? Did you forget to activate a virtual environment?
2022-01-14T04:51:30.414522+00:00 heroku[web.1]: Process exited with status 1
2022-01-14T04:51:30.500903+00:00 heroku[web.1]: State changed from starting to crashed

这也在日志中:

2022-01-14T08:48:31.449379+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/" host=vygrapp.herokuapp.com request_id=dfd73435-79ec-4925-9206-f40eabededfd fwd="61.68.159.52" dyno= connect= service= status=503 bytes= protocol=https
2022-01-14T08:48:31.770207+00:00 heroku[router]: at=error code=H10 desc="App crashed" method=GET path="/favicon.ico" host=vygrapp.herokuapp.com request_id=6bdac394-8932-492b-b0b2-f9f4785a23de fwd="61.68.159.52" dyno= connect= service= status=503 bytes= protocol=https

这是我的Procfile的内容:

web: python manage.py runserver 0.0.0.0:$PORT

更新后 requirements.txt 我遇到了这个问题:

remote: -----> $ python manage.py collectstatic --noinput
remote:        Traceback (most recent call last):
remote:          File "/tmp/build_4a60c6e6/manage.py", line 22, in <module>
remote:            main()
remote:          File "/tmp/build_4a60c6e6/manage.py", line 18, in main
remote:            execute_from_command_line(sys.argv)
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 425, in execute_from_command_line
remote:            utility.execute()
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/core/management/__init__.py", line 401, in execute
remote:            django.setup()
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/__init__.py", line 24, in setup
remote:            apps.populate(settings.INSTALLED_APPS)
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/apps/registry.py", line 114, in populate
remote:            app_config.import_models()
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/apps/config.py", line 300, in import_models
remote:            self.models_module = import_module(models_module_name)
remote:          File "/app/.heroku/python/lib/python3.9/importlib/__init__.py", line 127, in import_module
remote:            return _bootstrap._gcd_import(name[level:], package, level)
remote:          File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
remote:          File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
remote:          File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
remote:          File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
remote:          File "<frozen importlib._bootstrap_external>", line 850, in exec_module
remote:          File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/auth/models.py", line 3, in <module>
remote:            from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/auth/base_user.py", line 47, in <module>
remote:            class AbstractBaseUser(models.Model):
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/base.py", line 122, in __new__
remote:            new_class.add_to_class('_meta', Options(meta, app_label))
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/base.py", line 326, in add_to_class
remote:            value.contribute_to_class(cls, name)
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/db/models/options.py", line 207, in contribute_to_class
remote:            self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/connection.py", line 15, in __getattr__
remote:            return getattr(self._connections[self._alias], item)
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/utils/connection.py", line 62, in __getitem__
remote:            conn = self.create_connection(alias)
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/db/utils.py", line 204, in create_connection
remote:            backend = load_backend(db['ENGINE'])
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/db/utils.py", line 111, in load_backend
remote:            return import_module('%s.base' % backend_name)
remote:          File "/app/.heroku/python/lib/python3.9/importlib/__init__.py", line 127, in import_module
remote:            return _bootstrap._gcd_import(name[level:], package, level)
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/gis/db/backends/postgis/base.py", line 6, in <module>
remote:            from .features import DatabaseFeatures
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/gis/db/backends/postgis/features.py", line 1, in <module>
remote:            from django.contrib.gis.db.backends.base.features import BaseSpatialFeatures
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/gis/db/backends/base/features.py", line 3, in <module>
remote:            from django.contrib.gis.db import models
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/gis/db/models/__init__.py", line 3, in <module>
remote:            import django.contrib.gis.db.models.functions  # NOQA
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/gis/db/models/functions.py", line 3, in <module>
remote:            from django.contrib.gis.db.models.fields import BaseSpatialField, GeometryField
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/gis/db/models/fields.py", line 3, in <module>
remote:            from django.contrib.gis import forms, gdal
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/gis/forms/__init__.py", line 3, in <module>
remote:            from .fields import (  # NOQA
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/gis/forms/fields.py", line 2, in <module>
remote:            from django.contrib.gis.gdal import GDALException
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/gis/gdal/__init__.py", line 28, in <module>
remote:            from django.contrib.gis.gdal.datasource import DataSource
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/gis/gdal/datasource.py", line 40, in <module>
remote:            from django.contrib.gis.gdal.driver import Driver
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/gis/gdal/driver.py", line 5, in <module>
remote:            from django.contrib.gis.gdal.prototypes import ds as vcapi, raster as rcapi
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/gis/gdal/prototypes/ds.py", line 9, in <module>
remote:            from django.contrib.gis.gdal.libgdal import GDAL_VERSION, lgdal
remote:          File "/app/.heroku/python/lib/python3.9/site-packages/django/contrib/gis/gdal/libgdal.py", line 53, in <module>
remote:            lgdal = CDLL(lib_path)
remote:          File "/app/.heroku/python/lib/python3.9/ctypes/__init__.py", line 374, in __init__
remote:            self._handle = _dlopen(self._name, mode)
remote:        OSError: /opt/homebrew/opt/gdal/lib/libgdal.dylib: cannot open shared object file: No such file or directory
remote: 
remote:  !     Error while running '$ python manage.py collectstatic --noinput'.
remote:        See traceback above for details.
remote: 
remote:        You may need to update application code to resolve this error.
remote:        Or, you can disable collectstatic for this application:
remote: 
remote:           $ heroku config:set DISABLE_COLLECTSTATIC=1
remote: 
remote:        https://devcenter.heroku.com/articles/django-assets
remote:  !     Push rejected, failed to compile Python app.
remote: 
remote:  !     Push failed
remote: Verifying deploy...
remote: 
remote: !       Push rejected to vygrapp.

您的 Procfile 无效

你必须改变

web: python manage.py runserver 0.0.0.0:$PORT

web: gunicorn your_django_progect_name.wsgi:application --log-file - --log-level debug

Be sure to add gunicorn to your requirements.txt file as well. read this