栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

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

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

《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

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1018701.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号