You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Django应用部署Heroku遇pre-receive hook declined错误求助

Django部署Heroku失败:pre-receive hook declined & collectstatic错误

错误详情

推送代码时收到以下拒绝提示:

! [remote rejected] main -> main (pre-receive hook declined)
error: failed to push some refs to 'https://git.heroku.com/immense-brushlands'

核心失败原因是构建阶段执行python manage.py collectstatic --noinput时出错,提示找不到js/canvas-to-blob.min.js.map和js/load-image.all.min.js.map文件,同时伴随代码版本重复构建警告。

项目已配置文件:

  • requirements.txt:包含Django、gunicorn、django-heroku、whitenoise等完整依赖
  • Procfile:web : gunicorn foodhub.wsgi

解决步骤

1. 修复缺失的静态源映射文件

这些.map文件是前端库的源映射文件,问题出在静态资源引用了它们但文件不存在:

  • 检查项目模板或静态资源配置,确认是否真的需要这些源映射。若不需要,直接移除对应引用。
  • 若必须保留,从对应前端库的官方包中下载缺失的.map文件,放到项目static/js/目录下,同时确保.gitignore没有排除.map文件,让Git能追踪到这些文件。

2. 临时跳过collectstatic(不推荐生产环境使用)

如果需要紧急部署,可临时禁用collectstatic步骤:

heroku config:set DISABLE_COLLECTSTATIC=1

注意:生产环境必须补全静态文件配置,否则静态资源无法正常加载。

3. 优化静态文件配置

  • 确保django-heroku正确生效,在settings.py末尾添加:
import django_heroku
django_heroku.settings(locals())
  • 确认settings.py中静态文件路径配置正确:
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')
STATIC_URL = '/static/'
STATICFILES_DIRS = [
    os.path.join(BASE_DIR, 'static'),
]
  • 配置Whitenoise托管静态文件,在settings.py的MIDDLEWARE列表中添加(放在SecurityMiddleware之后,CommonMiddleware之前):
'whitenoise.middleware.WhiteNoiseMiddleware',

4. 消除重复构建警告

  • 检查项目根目录,将node_modules等前端构建缓存目录添加到.gitignore,避免不必要的文件被提交。
  • 确认Heroku仅使用Python buildpack:
heroku buildpacks:set heroku/python

内容的提问来源于stack exchange,提问作者mick

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.17 01:05:18