Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask - POST - The method is not allowed for the requested URL

Tags:

python

post

flask

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.

like image 928
Phil27 Avatar asked Jan 18 '16 10:01

Phil27


People also ask

How do you handle a post request in python flask?

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.


1 Answers

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/

like image 141
Anarkopsykotik Avatar answered Oct 01 '22 15:10

Anarkopsykotik