我有 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 应该为我执行此操作,但我不确定为什么没有。这是预期的行为吗?我需要自己做吗?
最佳答案
奇怪,还没有人回答你!只需将其添加到您的 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
关于python - Heroku 在部署时按预期执行 collectstatic?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14253978/