Flask request.form - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Web Scraping & Web Development (https://python-forum.io/forum-13.html) +--- Thread: Flask request.form (/thread-32280.html) |
Flask request.form - ifigazsi - Feb-01-2021 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> RE: Flask request.form - ifigazsi - Feb-02-2021 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)' #etcSeems to work :) RE: Flask request.form - buran - Feb-02-2021 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: here I use flash message to display the result of button click
|