I just started learning Flask but I meet troubles with the POST method.
Here is my (very simple) Python code :
@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
I successfully can submit my form by clicking on the send button but I a 405 error is returned :
Method Not Allowed The method is not allowed for the requested URL.
It is a pretty simple case, but I cannot understand where is my mistake.
GET and POST requestsEnter the following script in the Python shell. Once the development server is up and running, open login. html in the browser, enter the name in the text field, and then click Submit. The form data will POST to the URL in the action clause of the form label.
You gotta add "POST" in the route declaration accepted methods. You've put it in the function.
@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")
See : http://flask.pocoo.org/docs/0.10/quickstart/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With