Python Flask - POST - 请求的 URL 不允许使用该方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34853033/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Flask - POST - The method is not allowed for the requested URL
提问by Phil27
I just started learning Flask but I meet troubles with the POST method.
我刚开始学习 Flask,但在使用 POST 方法时遇到了麻烦。
Here is my (very simple) Python code :
这是我的(非常简单的)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")
when going to : http://127.0.0.1:5000/test
什么时候去:http: //127.0.0.1: 5000/test
I successfully can submit my form by clicking on the send button but I a 405 error is returned:
我可以通过单击发送按钮成功提交表单,但返回了405 错误:
Method Not Allowed The method is not allowed for the requested URL.
不允许的方法 请求的 URL 不允许使用该方法。
It is a pretty simple case, but I cannot understand where is my mistake.
这是一个非常简单的案例,但我无法理解我的错误在哪里。
采纳答案by Anarkopsykotik
You gotta add "POST" in the route declaration accepted methods. You've put it in the function.
您必须在路由声明接受的方法中添加“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")