python - Django 应用程序的 Procfile 中应该包含哪些内容?

标签 python django heroku procfile dyno

对于 Heroku 上的 Django 应用程序,我的 Procfile 中应该包含什么?

我试过:

web: python appname.py

因为我找到了一个类似 python 应用程序的示例。

除了我可能需要使用 gunicorn 而不是 python 之外,进一步的搜索并没有让事情变得更清楚。我发现各种建议各种格式的帖子,例如:

web gunicorn
web:gunicorn
web: gunicorn

我不知道 gunicorn 之后会发生什么,有些帖子有编程语言,有些有 IP 地址,有些还有其他各种东西。

有人建议运行:

heroku ps:scale web=1

但这会导致错误:

Scaling dynos... !
 !    Couldn't find that process type (web).

我只是没有头绪,不知道该去哪里。

自发布以来,我观看了一些关于此的视频并尝试:

web: gunicorn appname.wsgi

在我的 Procfile 中,但它仍然不起作用,仍然导致:

at=error code=H14 desc="No web processes running"

最佳答案

您需要 3 个文件才能成功将 Django 应用程序部署到 Heroku。

  • 程序文件
  • runtime.txt
  • requirements.txt

这 2 个模块应该在您的 requirements.txt 中

Procfile 中,输入

release: python manage.py migrate
web: gunicorn yourprojectname.wsgi
  • 第一行解释了部署的类型 release,这意味着生产版本,然后是 migrate,我想你知道会做什么。

  • 第二行说明 Gunicorn 是 the Python WSGI HTTP Server for UNIX

  • runtime.txt 中像这样输入你的 python 版本

    python-3.9.6

您可以使用此终端命令python --version

查看您的 python 版本
  • 最后您将需要 requirements.txt,您可以使用 pip freeze > requirements.txt 生成它,同时激活 venv 以防万一您在虚拟环境中操作。

项目启动步骤,最简单的方法:

  • 停用你的 venv 以防万一
  • 转到 heroku 仪表板
  • 创建一个应用并选择一个有意义的名称和免费计划,这样您就可以免费运行 1 个工作器
  • 复制其 url:herokuappname.herokuapp.com 然后在 settings.py 中,将其粘贴到 ALLOWED_HOSTS = ['herokuappname.herokuapp.com']
  • 设置DEBUG = False,在生产环境中不允许调试
  • 然后在您的终端导航到项目文件夹 并一一输入以下命令
  • Heroku login # 登录到您的 Heroku 帐户
  • heroku git:remote -a yourAppname # 连接到您已经创建的应用
  • git init # 初始化你的仓库
  • git add . # 添加所有项目的文件到初始化的repo
  • git commit -m "第一次推送" # commit
  • git push heroku master # 将项目文件推送到远程 Heroku 应用程序仓库

部署成功后,输入-回车

  • heroku logout # 退出

您要使用哪种存储?

因为 Heroku 不托管静态文件。

  • 您可以免费使用 Azure 存储。完整教程here

这是一个 project我的模型部署到带有 azure 存储的 Heroku,您将找到所有需要的详细信息。

关于python - Django 应用程序的 Procfile 中应该包含哪些内容?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69605603/

相关文章:

python - 将用户值传递给 WHERE 子句

python - 如何在 Python 中从另一个字典复制唯一的键和值

python - Django 性能 |在 Django 中更新模型后重新计算所有相关模型中的字段值

python - 在 Django View 中将一页重定向到另一页时如何延迟 5 秒

python - Ubuntu 10.10 上的 virtualenvwrapper - Python

ruby-on-rails - Rails、Heroku 和 Resque : Workers stuck in infinite "working" state

heroku - 将 fork 项目从 Heroku 迁移到 Github

python - 将列(str)转换为(Float),ValueError : could not convert string to float: 'Null'

python - Kivy Clear_widget 奇怪的行为(附有可重现的代码)

用于安装 libmosquitto 的 heroku buildpack