Flask - POST - 请求的URL不允许使用该方法

Phi*_*l27 5 python post flask

我刚刚开始学习Flask,但我遇到了POST方法的麻烦.

这是我的(非常简单的)Python代码:

@app.route('/test')
def test(methods=["GET","POST"]):
    if request.method=='GET':
        return('<form action="/test" method="post"><input type="submit" value="Send" /></form>')

    elif request.method=='POST':
        return "OK this is a post method"
    else:
        return("ok")
Run Code Online (Sandbox Code Playgroud)

什么时候去:http://127.0.0.1: 5000/test

我成功地可以通过单击发送按钮提交我的表单,但我返回405错误:

方法不允许所请求的URL不允许使用该方法.

这是一个非常简单的案例,但我无法理解我的错误在哪里.

Ana*_*tik 10

你必须在路由声明接受的方法中添加"POST".你把它放在了这个功能中.

@app.route('/test', methods=['GET', 'POST'])
def test():
    if request.method=='GET':
        return('<form action="/test" method="post"><input type="submit" value="Send" /></form>')

    elif request.method=='POST':
        return "OK this is a post method"
    else:
        return("ok")
Run Code Online (Sandbox Code Playgroud)

请参阅:http://flask.pocoo.org/docs/0.10/quickstart/