Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Flask request.form
#1
Hi!

I have three buttons on new_page.html
Only the first one is working . Add something button.
How to fix it?
I guess try-expect blocks are not to pythonic.

from flask import Flask, render_template, request

app = Flask(__name__)

@app.route('/', methods=['POST', 'GET'])
def index():
    return render_template('index.html')

@app.route('/new_page', methods=['POST', 'GET'])
def new_page():

    if request.method == 'POST':
        if request.form['add'] == 'Add something':
            print('Adding something')
            return render_template('new_page.html')
        if request.form['list'] == 'List something':
            print('Listing something')
            return render_template('new_page.html')
        if request.form['del'] == 'Delete something':
            print('Deleting something')
            return render_template('new_page.html')

    return render_template('new_page.html')


if __name__ == '__main__':
    app.run()
index.html:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
hello world this is the index page

<form method="GET" action="{{ url_for('new_page') }}">
<button style="font-size: 30px; padding: 5px" type="submit">To new page</button>
</form>
</body>
</html>

********************************************
new_page.html:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
this is the new page
<br>
<form>
<input type="submit" id="send" name="add" formmethod="post" value="Add something">
<input type="submit" id="send" name="list" formmethod="post" value="List something">
<input type="submit" id="send" name="del" formmethod="post" value="Delete something">
</form>
</body>
</html>
Reply
#2
if request.method == 'POST':
    add_button = request.form.get('add')
    list_button = request.form.get('list')
    del_button = request.form.get('del')
    if add_button:
        print('Add something button was pressed)'
    if del_button:
        print('Delete something button was pressed)'
#etc
Seems to work :)
Reply
#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


Possibly Related Threads…
Thread Author Replies Views Last Post
  Flask: editing a submitted form test 1 1,701 Jun-07-2022, 10:37 AM
Last Post: test
  Flask Basics request.form ifigazsi 0 1,783 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,135 Jan-09-2021, 06:08 PM
Last Post: eraosa
  POST request with form data issue web scraping hoff1022 1 2,649 Aug-14-2020, 10:25 AM
Last Post: kashcode
  Jinja sort values from request.form SpongeB0B 2 2,183 Jul-26-2020, 07:41 AM
Last Post: SpongeB0B
  got some problem with flask signup form. darktitan 1 2,213 Aug-30-2019, 06:05 AM
Last Post: aligoren
  how i save the html form to flask database mebaysan 1 7,244 Feb-07-2019, 12:56 AM
Last Post: snippsat
  Flask rest api How to retrieve json request raysefo 4 6,011 Jan-20-2019, 06:46 PM
Last Post: raysefo
  Flask redirect(request.url) returns None iFunKtion 1 10,418 Nov-27-2018, 10:50 PM
Last Post: nilamo
  request.form[ Dynamic Value]. How to? KirkmanJ 0 2,298 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