5

I am getting this error when I try to submit a request.

Method Not Allowed

The method is not allowed for the requested URL.

And here is my flask code..

@app.route("/")
def hello():
  return render_template("index.html")

@app.route("/", methods=['POST','GET'])
def get_form():
  query = request.form["search"]
  print query

And my index.html

<body>

<div id="wrap">
  <form action="/" autocomplete="on" method="POST">
    <input id="search" name="search" type="text" placeholder="How are you feeling?">
     <input id="search_submit" value="Send" type="submit">
  </form>
</div>

  <script src="js/index.js"></script>

</body>

Edit.. My complete flask code:

from flask import  Flask,request,session,redirect,render_template,url_for
import flask
print flask.__version__
app = Flask(__name__)

@app.route("/")
def entry():
    return render_template("index.html")

@app.route("/data", methods=['POST'])
def entry_post():
    query = request.form["search"]
    print query
    return render_template("index.html")


if __name__ == "__main__":
    app.run()
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
frazman
  • 32,081
  • 75
  • 184
  • 269
  • Why does the second `/` route handle `GET` **at all** when the first one already handles `GET`? This is confusing Flask no end. – Martijn Pieters Jun 06 '14 at 18:05
  • @MartijnPieters: I was just experimenting.. on how to get this thing to work.. I tried with just post as well.. but same issue – frazman Jun 06 '14 at 18:06

1 Answers1

5

You are posting to the entry() function, while your entry_post() function listens to a different route; it is registered to only listen to /data, not /:

@app.route("/data", methods=['POST'])
def entry_post():

The / route doesn't accept POST, by default only GET, HEAD and OPTIONS are allowed.

Adjust your form accordingly:

<form action="/data" autocomplete="on" method="POST">

Take into account that Flask does not reload your source unless you enable debugging:

app.run(debug=True)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343