小编典典

Flask-不允许使用POST错误405方法

flask

我刚刚开始学习Flask,并且我正在尝试创建一种允许POST方法的表单。

这是我的方法:

@app.route('/template', methods=['GET', 'POST'])
def template():
    if request.method == 'POST':
        return("Hello")
    return render_template('index.html')

而我的index.html

<html>

<head>
  <title> Title </title>
</head>

<body>
  Enter Python to execute:
  <form action="/" method="post">
    <input type="text" name="expression" />
    <input type="submit" value="Execute" />
  </form>
</body>

</html>

加载表单(在收到GET时将其呈现)可以正常工作。但是,当我单击“ 提交”按钮时,出现一个POST 405 error Method Not Allowed

为什么不显示“ Hello”?


阅读 1637

收藏
2020-04-05

共1个答案

小编典典

除非存在输入错误,否则你的表单将提交给/路由方法,/template除非你输入错误,否则应调整表单的action属性以指向template视图:action="{{ url_for('template') }}"

2020-04-05