不能将PUT方法与烧瓶一起使用

时间:2017-01-26 07:09:26

标签: python flask put

我写了这个简单的程序:

@app.route('/puttest/', methods=['GET', 'PUT'])
def upload_file():
    if request.method == 'PUT':
        return 'Hello, {}!'.format(request.form['name'])
    else:
        return '''
            <title>Does it work ?</title>
            <h1>PUT test</h1>
            <form action=http://localhost:8887/puttest/ method=put>
                <input type=text name=name>
                <input type=submit value=try>
            </form>

        '''

if __name__ == '__main__':
    app.run('0.0.0.0', 8887)

它适用于GET方法,但它不适用于PUT。尝试发送put消息,我可以在浏览器中看到此错误:

Method Not Allowed

The method is not allowed for the requested URL.

put方法发生了什么?

如果我在程序中的put处更改post方法,它将正常工作。

1 个答案:

答案 0 :(得分:5)

PUT不能使用HTML方法属性。 允许的值为:method = get | post

你必须在Webforms中使用POST:

@app.route('/puttest/', methods=['GET', 'POST'])
def upload_file():
if request.method == 'POST':
    return 'Hello, {}!'.format(request.form['name'])
else:
    return '''
        <title>Does it work ?</title>
        <h1>PUT test</h1>
        <form action=http://localhost:8887/puttest/ method=post>
            <input type=text name=name>
            <input type=submit value=try>
        </form>
    '''

更多信息:Using PUT method in HTML formHTML Standard