Flask framework is using GET instead of specifying POST method

Viewed 664

I'm trying to add data in dictionary through form using post method. But it's not working. form is working with get method but not post. I read many similar posts but could'nt understand so posting here. Please help me to locate the problem and solve it.

Python Code

@app.route('/add_card',methods=["GET","POST"])
def add_card():
    if request.method == "POST":
        card = {"question": request.form['question'],
                "answer": request.form['answer']}
        db.append(card)
        return redirect(url_for('card_view',index=len(db)-1))

    else:
        return render_template("add_card.html")

Form code:

 <form methods=" POST">
        <p>
            Question:
            <input type="text" name="=question">
        </p>
        <p>
            Answer:
            <input type="text" name="'answer">
        </p>
        <button type="submit">
            Create
        </button>
    </form>
2 Answers

Replace:

<form methods=" POST">

with:

<form method="POST">

You have an extra s, making the form use GET to submit.

As an aside, if you have many forms in your app, Flask-WTF may become handy.

<form action="{{ url_for('add_card') }}" method="post">
     <p>
        Question:
        <input type="text" name="question">
    </p>
    <p>
        Answer:
        <input type="text" name="answer">
    </p>
    <button type="submit">
        Create
    </button>
</form>

Use <form method="POST"> Instead of <form methods="POST"> .

Its better use url-binding https://flask.palletsprojects.com/en/1.1.x/quickstart/#url-building

Related