Home > Net >  Keep getting HTTP 405 Method Not Allowed error when trying to POST form data with Flask
Keep getting HTTP 405 Method Not Allowed error when trying to POST form data with Flask

Time:11-06

I am trying to capture HTTP form data using Python and Flask, but I keep getting "method not allowed" error. I have been trying to figure out why for some time with no luck. Here is what my code looks like:

@app.route("/add_recipe")  
  def addrecipepage():
    if request.method=="post":
        print ("Successful post request") # Just testing if code is working so far
    return render_template("add_recipe.html")
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

and the HTML code:

<form id="contact" action="browseAll" method="post">
    <div class="row">
      <div class="col-md-6">
        <fieldset>
          <input name="name" type="text" class="form-control" id="name" placeholder="Recipe Name..." required="">
        </fieldset>

<div class="col-12">
    <textarea name="demo-message" id="recipeText" placeholder="Enter Ingredients & Instructions Here" rows="15"></textarea>
  </div>
<div class="col-md-12">
    <button type="submit" id="form-submit" class="button">Add The Recipe!</button>
    <button id="uploadPhotoButton">Upload A Photo!</button>
    </div>
</form>
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

When I click on the submit button, I get a HTTP 405 Method Not Allowed error. Anybody have any ideas as to why after looking at this? "browseAll" is another HTML page that I created an endpoint for in my app.py file. The same error gets thrown even if I don't specify an action though.

CodePudding user response:

You are doing a POST (your form has method = "post") but your route does not have a post method attached to it. When you define a route without attaching a method, it defaults to GET. You need to do this

@app.route("/add_recipe", methods =['GET', 'POST'])
  • Related