Flask Python Buttons

2019-01-30 04:52发布

Im 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?

6条回答
做自己的国王
2楼-- · 2019-01-30 05:25

Apply (different) name attribute to both buttons like

<button name="one">

and catch them in request.data.

查看更多
萌系小妹纸
3楼-- · 2019-01-30 05:32

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" />

            </div>
        </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()
查看更多
可以哭但决不认输i
4楼-- · 2019-01-30 05:34

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)
查看更多
等我变得足够好
5楼-- · 2019-01-30 05:35

The appropriate way for doing this:

Put watch and download buttons into your template.

@app.route('/')
def index()
  if form.validate_on_submit():
    if 'download' in request.form:
       pass  
    elif 'watch' in request.form:
       pass ... etc
查看更多
倾城 Initia
6楼-- · 2019-01-30 05:44

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')
查看更多
祖国的老花朵
7楼-- · 2019-01-30 05:45

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

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

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

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

查看更多
登录 后发表回答