Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flask request.form
#3
I would say the way to handle this is to make all submit controls/buttons have the same name and different value.

Then just act based on what value has been submitted.

from flask import Flask, render_template, request, flash
 
app = Flask(__name__)
app.config['SECRET_KEY'] = ',9fB3L,,b&h~?^dy' # as the name suggest this should be a secret.
 
@app.route('/', methods=['POST', 'GET'])
def index():
    return render_template('index.html')
 
@app.route('/new_page', methods=['POST', 'GET'])
def new_page():
    actions = {'Add something': 'Adding something',
               'List something': 'Listing something',
               'Delete something': 'Deleting something'}
    if request.method == 'POST':
            flash(actions[request.form['my_btn']])

    return render_template('new_page.html')
 
 
if __name__ == '__main__':
    app.run()
new_page.html:
Output:
<html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> {% with messages = get_flashed_messages() %} {% if messages %} <ul class=flashes> {% for message in messages %} <li>{{ message }}</li> {% endfor %} </ul> {% endif %} {% endwith %} this is the new page <br> <form> <input type="submit" id="send" name="my_btn" formmethod="post" value="Add something"> <input type="submit" id="send" name="my_btn" formmethod="post" value="List something"> <input type="submit" id="send" name="my_btn" formmethod="post" value="Delete something"> </form> </body> </html>
here I use flash message to display the result of button click
ifigazsi likes this post
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Messages In This Thread
Flask request.form - by ifigazsi - Feb-01-2021, 01:49 PM
RE: Flask request.form - by ifigazsi - Feb-02-2021, 08:03 AM
RE: Flask request.form - by buran - Feb-02-2021, 11:56 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Flask: editing a submitted form test 1 1,763 Jun-07-2022, 10:37 AM
Last Post: test
  Flask Basics request.form ifigazsi 0 1,811 Feb-09-2021, 09:05 AM
Last Post: ifigazsi
  Using Python request without selenium on html form with javascript onclick submit but eraosa 0 3,195 Jan-09-2021, 06:08 PM
Last Post: eraosa
  POST request with form data issue web scraping hoff1022 1 2,705 Aug-14-2020, 10:25 AM
Last Post: kashcode
  Jinja sort values from request.form SpongeB0B 2 2,243 Jul-26-2020, 07:41 AM
Last Post: SpongeB0B
  got some problem with flask signup form. darktitan 1 2,273 Aug-30-2019, 06:05 AM
Last Post: aligoren
  how i save the html form to flask database mebaysan 1 7,314 Feb-07-2019, 12:56 AM
Last Post: snippsat
  Flask rest api How to retrieve json request raysefo 4 6,131 Jan-20-2019, 06:46 PM
Last Post: raysefo
  Flask redirect(request.url) returns None iFunKtion 1 10,505 Nov-27-2018, 10:50 PM
Last Post: nilamo
  request.form[ Dynamic Value]. How to? KirkmanJ 0 2,336 Aug-01-2018, 12:14 PM
Last Post: KirkmanJ

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020