Submission button is not working when it's coonnected to MySQL database using flask framework

Issue

The submission button is not working properly. I’m getting the desired output after submitting the form. Kindly let me know where I’m going wrong. Find the attached .html and .py code below. Also, let me know what’s the difference between using "button" tag and "input" tag as buttons in html?

main.py

mysql=MySQL(app)

@app.route("/",methods=["GET","POST"])
def insert():
    cur=mysql.connection.cursor()
    if request.form=='POST':
        print(request.form["description"])
    cur.execute("select * from test.task")
    task=cur.fetchall()
    cur.close()
    return render_template("base.html",task=task)

if __name__=="__main__":
    app.run(debug=True, port=8000)

base.html

<body>
    <h3>Table</h3>

    {% block body %}
    <div>
        <form action="/" method="POST">
            <label for="description">Description</label>
            <input type="text" name="description" id="description" placeholder="Description Input" required>
            <button type="submit">Submit</button>
        </form>
    </div>
    <br>
    <div class="=Task">
        <table>
            <tr>
                <th>S.No.</th>
                <th>Description</th>
                <th>Date and time</th>
                <th>Action</th>
            </tr>

            {% for task in task %}
            <tr>
                <td>{{loop.index}}</td>
                <td>{{task.1}}</td>
                <td>{{task.2}}</td>
                <td><a href="Update">Update</a> <a href="Delete">Delete</a>
                </td>
            </tr>
            {% endfor %}



        </table>
    </div>
    {% endblock body %}

</body>

</html>

Solution

Try replacing this with:

if request.form == "POST":

with

if request.method == "POST":

Answered By – charchit

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published