将 Cookiecutter-Django 应用程序部署到 Heroku 时出错

Error deploying Cookiecutter-Django app to Heroku

我正在使用 Django Cookiecutter 样板,使用 Docker。在本地一切正常,所以现在我正在尝试按照本指南将应用程序部署到 Heroku:Deployment on Heroku。我正在使用 Django 3 和 Rest 框架。 Docker 用于开发。

运行配置命令后,我推送到 Heroku master 并收到此错误:

Error while running '$ python manage.py collectstatic --noinput'.

跟踪如下:

remote: -----> $ python manage.py collectstatic --noinput
remote:        Traceback (most recent call last):
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/environ/environ.py", line 273, in get_value
remote:            value = self.ENVIRON[var]
remote:          File "/app/.heroku/python/lib/python3.7/os.py", line 679, in __getitem__
remote:            raise KeyError(key) from None
remote:        KeyError: 'SENDGRID_API_KEY'
remote:        During handling of the above exception, another exception occurred:
remote:        Traceback (most recent call last):
remote:          File "manage.py", line 31, in <module>
remote:            execute_from_command_line(sys.argv)
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
remote:            utility.execute()
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
remote:            self.fetch_command(subcommand).run_from_argv(self.argv)
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/django/core/management/__init__.py", line 231, in fetch_command
remote:            settings.INSTALLED_APPS
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/django/conf/__init__.py", line 76, in __getattr__
remote:            self._setup(name)
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/django/conf/__init__.py", line 63, in _setup
remote:            self._wrapped = Settings(settings_module)
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/django/conf/__init__.py", line 142, in __init__
remote:            mod = importlib.import_module(self.SETTINGS_MODULE)
remote:          File "/app/.heroku/python/lib/python3.7/importlib/__init__.py", line 127, in import_module
remote:            return _bootstrap._gcd_import(name[level:], package, level)
remote:          File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
remote:          File "<frozen importlib._bootstrap>", line 983, in _find_and_load
remote:          File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
remote:          File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
remote:          File "<frozen importlib._bootstrap_external>", line 728, in exec_module
remote:          File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
remote:          File "/tmp/build_027dd19e3113d3ba2634e63f0f93e673/config/settings/production.py", line 128, in <module>
remote:            "SENDGRID_API_KEY": env("SENDGRID_API_KEY"),
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/environ/environ.py", line 123, in __call__
remote:            return self.get_value(var, cast=cast, default=default, parse_default=parse_default)
remote:          File "/app/.heroku/python/lib/python3.7/site-packages/environ/environ.py", line 277, in get_value
remote:            raise ImproperlyConfigured(error_msg)
remote:        django.core.exceptions.ImproperlyConfigured: Set the SENDGRID_API_KEY environment variable

在错误下方,我收到消息以使用此命令禁用收集静态:

$ heroku config:set DISABLE_COLLECTSTATIC=1

我运行上面的命令并继续但是下一个命令不起作用因为Heroku找不到manage.py文件

heroku run python manage.py createsuperuser

我在网上搜索了各种部署指南,但看起来在 Heroku 上部署 Django 应用程序并不像在 Rails.

上那样简单

错误提示应用缺少环境变量SENDGRID_API_KEY。这听起来像是一个文档错误,如果您有时间,我将不胜感激 report it

基本上,Heroku 的部署假定您使用 Mailgun 作为电子邮件服务,因为它要求您 运行:

heroku addons:create mailgun:starter

但是,您似乎选择了 Sendgrid,这是最近引入的一个选项(参见 PR #2435),但此页面未相应更新。

要解决您的问题,您需要添加 Sendgrid add-on to your app. Once added, from Heroku, you should be able to click the add-on to access your Sendgrid dashboard where you need to create an API key as per this guide and add it the config vars of you Heroku app, following this guide