html – Flask Python Buttons

html – Flask Python Buttons

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)

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>

html – Flask Python Buttons

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 in request.form:
        pass
    return render_template(contact.html)

Simple, concise, and it works. Dont even need to instantiate a form object.

Leave a Reply

Your email address will not be published. Required fields are marked *