Belphegor Belphegor - 3 years ago 265
HTML Question

How can I make html submit button functional in flask?

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

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 Source

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...

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download