我的 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/