git部署上heroku出现报错
命令:git push heroku master
remote: raise ImproperlyConfigured(
remote: django.core.exceptions.ImproperlyConfigured: You're using the staticfiles app without having set the STATIC_ROOT setting to a filesystem path.
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 xxxxxxxxxx.
remote:
To https://git.heroku.com/xxxxxxxxxx.git
! [remote rejected] master -> master (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/xxxxxxxxxx.git'
原因是因为settings.py中if语句中的代码没有执行,导致项目无法正确生成
解决办法:
修改settings.py的代码
if os.getcwd() == '/app':
替换为
cwd = os.getcwd()
if cwd == '/app' or cwd[:4] == '/tmp':
使用whitenoise管理静态文件
用whilenoise替换dj-static、static3
(ll_env)learning_log$ pip uninstall dj-static
(ll_env)learning_log$ pip uninstall static3
(ll_env)learning_log$ pip install whitenoise
此外还需要再settings.py的中间件中添加设置。WhiteNoise中间件应该放在Django安全中间件SecurityMiddleware之后
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'whitenoise.middleware.WhiteNoiseMiddleware',
--snip--
]
然后去掉wsgi.py里的Cling函数,更新requirements.txt文件,重新部署即可。
(ll_env)learning_log$ pip freeze > requirements.txt
(ll_env)learning_log$ git commit -am "Updated project to use WhiteNoise."
(ll_env)learning_log$ git push heroku master
参考:
https://segmentfault.com/q/1010000010016708
http://ehmatthes.github.io/pcc/chapter_20/README.html#questions
http://whitenoise.evans.io/en/stable/django.html