Not excecuting the if statement in a flask file

Issue

The program is syntactically correct. There are no errors. But it’s not excecuting anything inside the if statement. Kindly let me know the reason with detailed explaintion of the below code segment.

main.py

@app.route("/update",methods=["GET","POST"])
def update():
    if request.method=="POST":
        print("Hello")

base.html

<form action="/" method="POST">
<label for="description">Description</label>
<input type="text", name="description",  id="description">
<button type="submit">Update</button>
</form>

Solution

Take a look at your base.html file again, no wonder the code in the function isn’t called, it’s not supposed to!

In the html file you have action="/", but in the Python file you define the route "/update". Here you can read up on the action attribute.

The correct html code would be:

<form action="/update" method="POST">
<label for="description">Description</label>
<input type="text", name="description",  id="description">
<button type="submit">Update</button>
</form>

Answered By – Bluenix

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