【问题标题】:Flask app works locally, but not on Heroku (Method not allowed)Flask 应用程序可以在本地运行,但不能在 Heroku 上运行(不允许使用方法)
【发布时间】:2017-06-28 20:36:57
【问题描述】:

我的 python 代码在本地运行时运行良好,但是当我点击表单上的提交按钮时在 Heroku 上失败,我收到错误:不允许的方法 请求的 URL 不允许该方法。

from flask import Flask, render_template, request, redirect
import dill

app = Flask(__name__)

@app.route('/',methods=['GET','POST'])
def main():
  return redirect('/index')

@app.route('/index',methods=['GET','POST'])
def index():
    message = '  '
    if request.method == 'GET':
        return render_template('index.html',output=message)
    elif request.method == 'POST':
      gender = request.form.get('sex')
      poverty = request.form.get('poverty')
      school = request.form.get('school')
      marital = request.form.get("marital")
      race = request.form.get("race")
      orient = request.form.get("orient")
      phys = request.form.get("phys")
      drinks = request.form.get("drinks")
      age = request.form.get("age")
      message = '  '
      output = get_risk([phys,orient,school,drinks,race,poverty,marital,gender,age])
      if output ==0:
        message = "Not At Risk"
      elif output == 1:
        message = "At Risk!"
      return render_template('index.html',output=message)

def get_risk(features):
  forest = dill.load(open('forest.pkd','rb'))
  return forest.predict(features)

有什么建议吗?

【问题讨论】:

  • 您好,欢迎来到 Stack Overflow,请花点时间通过 welcome tour 了解您在此处的方式(并获得您的第一个徽章),阅读如何创建 Minimal, Complete, and Verifiable example并检查How to Ask Good Questions,这样您就有机会获得反馈和有用的答案。
  • 确保您的 HTML 在表单元素上具有 method="POST" 属性,并且 action 属性指定了正确的路径。

标签: python heroku flask


【解决方案1】:

浏览器中的“不允许的方法”很常见,并不能真正告诉您太多信息 - 这是一种向不应查看您的代码或了解您的应用程序详细信息的用户隐藏错误详细信息的方法。尝试 heroku 日志以获得更详细的报告。即使 heroku 日志说我的应用程序正在 heroku 上运行,我也遇到了同样的错误

【讨论】:

    【解决方案2】:

    3 年零 8 个月后看到这个。我只是先创建了应用程序并测试了 heroku 创建的默认页面,但我得到了这个方法不允许。我的问题是使用了错误的链接。 git.heroku.com/yourapp.git 给出了这个错误。但是yourapp.herokuapp.com/ 链接有效。同样,这只是默认页面。

    如果您部署了应用并且错误存在于正确的 URL,您可能会看到应用程序错误页面,如前所述,您需要利用其仪表板上的 heroku 日志来解决这些问题。

    【讨论】:

      猜你喜欢
      • 2021-09-18
      • 2018-06-10
      • 1970-01-01
      • 1970-01-01
      • 2018-12-16
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 2019-05-24
      相关资源
      最近更新 更多