how to detect when a button is pressed in flask code example
Example 1: button in flask
<input type="submit" name="submit_button" value="Do Something">
<input type="submit" name="submit_button" value="Do Something Else">
def contact():
if request.method == 'POST':
if request.form['submit_button'] == 'Do Something':
pass
elif request.form['submit_button'] == 'Do Something Else':
pass
else:
pass
elif request.method == 'GET':
return render_template('contact.html', form=form)
Example 2: two buttons on html page flask
<form method ="post" action="/">
<button type="submit" name="submit_a" value="submit_a"> Submit_a </button>
<button type="submit" name="submit_b" value="submit_b"> Submit_b </button>
</form>
def submit():
if request.method == "POST":
if request.form.get("submit_a"):
elif request.form.get("submit_b"):
elif request.method == "GET":