Heroku 在部署时按预期执行collectstatic?

问题描述 投票:0回答:2

我设置了 Django 来收集静态文件并使用 django-storages 将它们复制到 S3 中,当我显式运行时,这就像一个魅力

heroku run python manage.py collectstatic

但是,我希望 Heroku 在部署时自动执行此操作(即 git Push 到 Heroku),如本文档中所述 https://devcenter.heroku.com/articles/django-assets 。显然,输出:

python manage.py collectstatic --dry-run --noinput

确定collectstatic是否配置正确(当我显式运行时collectstatic可以正常工作)。我得到:

$ heroku run python manage.py collectstatic --dry-run --noinput
Running `python manage.py collectstatic --dry-run --noinput` attached to terminal... up, run.9607
Pretending to copy '/app/app_pkg/static/robots.txt'
.... # Lot of similar outputs not shown
16 static files copied.

这对我来说看起来,但文档没有指定我应该看到什么。当我部署项目时,一切似乎都工作正常并成功完成。

-----> Installing dependencies using Pip (1.2.1)
       Cleaning up...
-----> Collecting static files
       16 static files copied.

-----> Discovering process types
       Procfile declares types -> web

但这些文件没有显示在我的 S3 存储桶中。如果我然后明确运行

heroku run python manage.py collectstatic

所有文件都显示在 S3 存储桶中。我认为 Heroku 应该为我执行此操作,但我不确定为什么不这样做。这是预期的行为吗?需要我自己做吗?

python django heroku
2个回答
7
投票

奇怪的是还没人回复你!只需将其添加到 Procfile 的开头即可:

web: python manage.py collectstatic --noinput

每次您的应用程序部署在 Heroku 上时,这都会运行 python 管理collectstatic。

最简洁的方法是将其链接到现有的 Procfile,如下所示:

web: python my_django_app/manage.py collectstatic --noinput; bin/gunicorn_django --workers=4 --bind=0.0.0.0:$PORT my_django_app/settings.py

感谢 Matthew Phiong 和 他精彩的博客文章


0
投票

建议人们使用在发布最佳答案后引入到heroku的release。 https://devcenter.heroku.com/articles/release-phase

release: python manage.py collectstatic --clear --noinput
© www.soinside.com 2019 - 2024. All rights reserved.