《python从入门到实践》中项目部署Heroku问题

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值