- Django Girls Tutorial: Extensionsが終わったのでHerokuにデプロイしてみる
tutorial-extensions.djangogirls.org
目次
- Django Girls Tutorialを参考に進める
- herokuはRuby on Rails以来で久しぶりに利用する
- djangogirls.orgのherokuページは情報が古そうだった
- 途中で簡単なPythonAnywhereを導入してherokuの方はあまり手をつけられてなそう気がする
- whitenoiseの設定をそのまま書くとエラーが出る
各ファイルの追加や修正
- requirements.txt
- Procfile
- herokuで使用するアプリケーションのプロセスタイプとエントリポイントを記述
- Gunicorn1を使用する
- wsgiのエントリポイントを記述
web: gunicorn mysite.wsgi --log-file -
- runtime.txt
- herokuで使用するpython version
- 古いと警告が出るらしい
python-3.7.4
- mysite/local_settings.py
- mysite/settings.py
- mysite/wsgi.py
heroku
- ログイン
- アプリケーションを名前をつけてcarete
- git commit後にherokuへpush
web processをstart、databaseをmigrateしたり
$ heroku ps:scale web=1
$ heroku run python manage.py migrate
# 管理画面のsuper userを作成
$ heroku run python manage.py createsuperuser
エラーログの確認
- URLにアクセスしたがエラーが出ているような画面が表示される
- herokuのlogを確認してみる
$ heroku logs --tail
django.core.exceptions.ImproperlyConfigured: WSGI application 'mysite.wsgi.application' could not be loaded; Error importing module.
Your WhiteNoise configuration is incompatible with WhiteNoise v4.0
This can be fixed by following the upgrade instructions at:
http://whitenoise.evans.io/en/stable/changelog.html#v4-0
- pipを確認すると入れているのはWhiteNoiseは5.0.1だったのだが…
- ここでgoogleや公式documentで調べたりする
- WhiteNoise4.0以降から書き方が変更となっているようだ
whitenoiseの記述修正
- mysite/wsgi.py ファイルの記述が問題のようだ
from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)
- settings.pyのMIDDLEWAREに以下を記述する
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
]
devcenter.heroku.com
再度deploy