Flask Python Buttons

PythonHtmlButtonFlask

Python Problem Overview


I'm trying to create two buttons on a page. Each one I would like to carry out a different Python script on the server. So far I have only managed to get/collect one button using.

def contact():
  form = ContactForm()

  if request.method == 'POST':
    return 'Form posted.'

  elif request.method == 'GET':
     return render_template('contact.html', form=form)

What would I need to change based on button pressed?

Python Solutions


Solution 1 - Python

Give your two buttons the same name and different values:

<input type="submit" name="submit_button" value="Do Something">
<input type="submit" name="submit_button" value="Do Something Else">

Then in your Flask view function you can tell which button was used to submit the form:

def contact():
    if request.method == 'POST':
        if request.form['submit_button'] == 'Do Something':
            pass # do something
        elif request.form['submit_button'] == 'Do Something Else':
            pass # do something else
        else:
            pass # unknown
    elif request.method == 'GET':
        return render_template('contact.html', form=form)

Solution 2 - Python

The appropriate way for doing this:

@app.route('/')
def index():
    if form.validate_on_submit():
        if 'download' in request.form:
            pass # do something
        elif 'watch' in request.form:
            pass # do something else

Put watch and download buttons into your template:

<input type="submit" name="download" value="Download">
<input type="submit" name="watch" value="Watch">

Solution 3 - Python

In case anyone was still looking and came across this SO post like I did.

<form>
    <input type="submit" name="open" value="Open">
    <input type="submit" name="close" value="Close">
</form>

def contact():
    if "open" in request.form:
        pass
    elif "close" in request.form:
        pass
    return render_template('contact.html')

Simple, concise, and it works. Don't even need to instantiate a form object.

Solution 4 - Python

I handle it in the following way:

<html>
	<body>

		<form method="post" action="/">

				<input type="submit" value="Encrypt" name="Encrypt"/>
				<input type="submit" value="Decrypt" name="Decrypt" />

		</form>
	</body>
</html>
    

Python Code :

    from flask import Flask, render_template, request
    
    
    app = Flask(__name__)
    
    
    @app.route("/", methods=['GET', 'POST'])
    def index():
        print(request.method)
        if request.method == 'POST':
            if request.form.get('Encrypt') == 'Encrypt':
                # pass
                print("Encrypted")
            elif  request.form.get('Decrypt') == 'Decrypt':
                # pass # do something else
                print("Decrypted")
            else:
                # pass # unknown
                return render_template("index.html")
        elif request.method == 'GET':
            # return render_template("index.html")
            print("No Post Back Call")
        return render_template("index.html")
    
    
    if __name__ == '__main__':
        app.run()

Solution 5 - Python

I think this solution is good:

@app.route('/contact', methods=['GET', 'POST'])
def contact():
    form = ContactForm()
    if form.validate_on_submit():
        if form.submit.data:
            pass
        elif form.submit2.data:
            pass
    return render_template('contact.html', form=form)

Form:

class ContactForm(FlaskForm):

    submit = SubmitField('Do this')
    submit2 = SubmitField('Do that')

Solution 6 - Python

Apply (different) name attribute to both buttons like

<button name="one">

and catch them in request.data.

Solution 7 - Python

this work to me .py

if request.method == "POST":
     if request.form.get('new Token'):
#something
     if request.form.get('custom Token'):
#something diferent

.html

<form method="post" >
<input type="submit" name="new Token" value="new Token">
<input type="submit" name="custom Token" value="custom Token">
</form>

works because the button that you do not oppress ,return None to python. nice day

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestiondojogeorgeView Question on Stackoverflow
Solution 1 - PythonMiguel GrinbergView Answer on Stackoverflow
Solution 2 - PythonAlexander.IljushkinView Answer on Stackoverflow
Solution 3 - PythonpoplyView Answer on Stackoverflow
Solution 4 - PythonrashidView Answer on Stackoverflow
Solution 5 - PythonslindenView Answer on Stackoverflow
Solution 6 - PythonPaweł PogorzelskiView Answer on Stackoverflow
Solution 7 - Pythonjero98772View Answer on Stackoverflow