10

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.

Phil27
  • 233
  • 1
  • 3
  • 12

1 Answers1

18

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/

Anarkopsykotik
  • 497
  • 5
  • 17