【问题标题】:Flask - POST Error 405 Method Not AllowedFlask - 不允许使用 POST 错误 405 方法
【发布时间】:2012-08-24 03:52:12
【问题描述】:

我刚开始学习 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 时呈现它)工作正常。然而,当我点击 submit 按钮时,我得到一个POST 405 error Method Not Allowed。

为什么不显示“Hello”?

【问题讨论】:

标签: python http post flask


【解决方案1】:

当方法被路由到/template 时,您的表单正在提交到/,除非这是一个错字,您应该调整表单的action 属性以指向template 视图:action="{{ url_for('template') }}"

【讨论】:

    【解决方案2】:

    替换:

     <form action="/" method="post">
    

    与:

     <form action="{{ url_for('template') }}" method="post">
    

    【讨论】:

      【解决方案3】:

      如果您省略 action 属性,表单将发布到当前 URL。

      替换:

      <form action="/" method="post">
      

      与:

      <form method="post">
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-02-16
        • 2019-07-05
        • 2013-12-05
        • 2012-10-26
        • 1970-01-01
        • 1970-01-01
        • 2017-10-16
        相关资源
        最近更新 更多