python - Flask 应用程序可以在本地运行,但不能在 Heroku 上运行(方法不允许)

标签 python heroku flask

我的 python 代码在本地运行时运行良好,但是当我点击表单上的提交按钮时在 Heroku 上运行失败,并且出现错误:Method Not Allowed The method is not allowed for the requested 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)

有什么建议吗?

最佳答案

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

关于python - Flask 应用程序可以在本地运行,但不能在 Heroku 上运行(方法不允许),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44812111/

相关文章:

python - SQLAlchemy 过滤不工作

python - Django HttpRequest问题

python - Pygame 将 2 个表面连接在一起

python - 在 Python 中为对象属性赋值的正确方法是什么?

python - Flask-Assets 在本地工作但不能在 Heroku 上工作

python - Flask 登录 AttributeError : 'User' object has no attribute 'is_active'

python - 如何在python中检测pdf是否是一列和两列

heroku - 解析服务器简单 Mailgun 适配器 'verifyUserEmails' 问题

node.js - Heroku 部署在 "Pruning devDependencies"步骤继续失败

python - 如何在 Flask 路由中保留前导零