Search code examples
djangodockerherokudjango-3.0cookiecutter-django

Error deploying Cookiecutter-Django app to Heroku


I'm working with the Django Cookiecutter boilerplate, using Docker. Everything works fine locally so now I'm trying to deploy the app to Heroku by following this guide: Deployment on Heroku. I'm using Django 3 and Rest framework. Docker for development.

After running the config commands, I push to Heroku master and get this error:

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

Here's the trace:

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

Beneath the error, I get the message to disable collect static with this command:

$ heroku config:set DISABLE_COLLECTSTATIC=1

I run the command above and proceed but the next command doesn't work because Heroku can't find the manage.py file

heroku run python manage.py createsuperuser

I've searched various deployment guides online but it looks like deploying a Django app on Heroku is not as straightforward as it is on Rails.


Solution

  • The error says that the app is missing the environment variable SENDGRID_API_KEY. That sounds like a documentation bug, if you have a minute I would appreciate if you could please report it.

    Basically, the Deployment for Heroku assumes that you're using Mailgun as email service, as it ask you to run:

    heroku addons:create mailgun:starter
    

    However, you seem to have selected Sendgrid, which is an option that was introduced recently (See PR #2435), but this page wasn't updated accordingly.

    To fix your problem, you need to add the 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.