Python Forum

Full Version: Flask request.form
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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>
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 :)
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