LoginSignup
34
23

More than 3 years have passed since last update.

HerokuにDjangoアプリをデプロイするときのエラーについての対処法

Last updated at Posted at 2019-04-30

はじめに

HerokuにDjangoアプリケーションをデプロイする際に、静的ファイルに関する環境変数を設定してあげないとエラーが出ますよという話です。

開発環境

OS:macOS Mojave(10.14.4)

対処法

Djangoのチュートリアルに従って、デプロイに関する様々な設定(HerokuがPythonアプリであると認識するためのrequirements.txtやWebサーバーに関して宣言するProcfileなど諸々の設定)をし終えて、いざデプロイしようと以下のコマンドを打ちエラーが出たことはありませんか。

remote: -----> $ python manage.py collectstatic --noinput
remote:        Traceback (most recent call last):
remote:          File "manage.py", line 21, in <module>
remote:            main()
remote:          File "manage.py", line 17, in main
remote:            execute_from_command_line(sys.argv)
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
remote:            utility.execute()
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/core/management/__init__.py", line 325, in execute
remote:            settings.INSTALLED_APPS
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 79, in __getattr__
remote:            self._setup(name)
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 66, in _setup
remote:            self._wrapped = Settings(settings_module)
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django/conf/__init__.py", line 157, in __init__
remote:            mod = importlib.import_module(self.SETTINGS_MODULE)
remote:          File "/app/.heroku/python/lib/python3.6/importlib/__init__.py", line 126, in import_module
remote:          File "/tmp/build_ec7d7e8811f7736c2961572f243025b3/config/settings.py", line 151, in <module>
remote:            django_heroku.settings(locals())
remote:          File "/app/.heroku/python/lib/python3.6/site-packages/django_heroku/core.py", line 89, in settings
remote:            config['STATIC_ROOT'] = os.path.join(config['BASE_DIR'], 'staticfiles')
remote:        KeyError: 'BASE_DIR'
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 hoge.
remote:
To https://git.heroku.com/hoge.git
 ! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/hoge.git'

これは静的ファイルを扱うための(詳しくは、アプリケーションごとのstaticディレクトリを指定ディレクトリに集約コピーする)機能をもつcollectstaticが不要であれば、環境変数DISABLE_COLLECTSTATICを設定してくださいと言われているので、以下のコマンドを実行します。

$ heroku config:set DISABLE_COLLECTSTATIC=1
Setting DISABLE_COLLECTSTATIC and restarting  hrtech360... done, v3
DISABLE_COLLECTSTATIC: 1

これで再度pushするとエラーが無くなりデプロイが完了するかと思います。

34
23
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
34
23