How can I make html submit button functional in flask?

Belphegor picture Belphegor · Jan 29, 2013 · Viewed 10.4k times · Source

I have a Flask application, and in my html template (which is Jinja2) I have a simple form which has only a button:

<form name='resetLayoutForm' method='POST'>
    <input type="submit" value="Reset layout" class="submitButton"/> 
</form>

Now, I want to know how can I 'let the Python script know' that the button was clicked?

I tried with:

@app.route('/localhost/some_route', methods = ['POST', 'GET'])
def function():
    .   .    .
    if request.method == 'POST':
        # code
    .   .    .

but this isn't a good solution, because I have some other operations in the code that are using the POST request method, and I don't want #code to be activated when I use these other POST operations.

Answer

Paul Collingwood picture Paul Collingwood · Feb 3, 2013

Duplicate the form that contains the button and set it's action to a different URL. Then, when pressed, only that different URL will receive the data.

Obviously, as others have noted, there are Javascript mechanisms to achieve this but if those are not needed...