【问题标题】:Heroku performing collectstatic as expected on deployment?Heroku在部署时按预期执行collectstatic?
【发布时间】:2013-01-10 08:55:08
【问题描述】:

我有 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 应该为我执行此操作,但我不确定为什么不这样做。这是预期的行为吗?我需要自己做吗?

【问题讨论】:

  • 有关similar thread 的更多信息。我也没有找到解决办法。
  • $ heroku labs:enable user-env-compile

标签: python django heroku


【解决方案1】:

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

web: python manage.py collectstatic --noinput

每次在 Heroku 上部署您的应用时,都会运行 python manage 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 和his awesome blog post

【讨论】:

  • 修改 Procfile 确实有效,现在仍然有效。谢谢。我现在使用构建的user-env-compile lab Heroku(Kenneth 提到)来支持在构建中包含环境变量(django-storages 需要),这样 Heroku 就可以执行静态资产收集,而无需修改 Procfile。
猜你喜欢
  • 2013-03-29
  • 2021-05-13
  • 1970-01-01
  • 2016-04-11
  • 2019-03-12
  • 2017-05-21
  • 2020-04-30
  • 2021-01-18
  • 1970-01-01
相关资源
最近更新 更多