2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

whitenoiseをインストールしてHerokuにデプロイしたときのエラーの対処法

Posted at

使用している教材によってはDjangoのアプリを動かすために必要なパッケージとしてwhitenoiseをインストールして使うように書かれてあります。

そのときに起きたエラーについて書きたいと思います。

古い教材ではwsgi.py に以下の内容を追加するように書かれています。

wsgi.py
from whitenoise.django import DjangoWhiteNoise
application = DjangoWhiteNoise(application)

そしてアプリを起動させようとするとこのようなエラーが出ます

ImportError:
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

2020年1月の最新のバージョンはwhitenoise 5.0でバージョンwhitenoise 4.0からDjangoでのWhiteNoiseを有効にする表記の仕方が変わったみたいです。

その書いてある通りに以下の文を追加します。

settings.py
MIDDLEWARE = [
  'django.middleware.security.SecurityMiddleware',
  'whitenoise.middleware.WhiteNoiseMiddleware',
  # ...
]
STATICFILES_STORAGE = 'whitenoise.storage.CompressedManifestStaticFilesStorage'

注意点はミドルウェアリストの一番上に追加することです。

そしてwsgi.pyに追加した分は消しましょう。

wsgi.py
from whitenoise.django import DjangoWhiteNoise #この行を削除します
application = DjangoWhiteNoise(application)   #この行を削除します 

これでうまく動くようになりました。

2
1
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
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?