Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

python - Multiple submit Buttons in Flask

I tried the method from this question: use many submit buttons in the same form

When i try this method i got:

if request.form.action == "one":
AttributeError: 'ImmutableMultiDict' object has no attribute 'action'

Can any on tell me what i'm doing wrong? '''python from flask import Flask, request, render_template,url_for from flask_wtf import FlaskForm from werkzeug.utils import redirect from wtforms import StringField, SubmitField

app = Flask(__name__)

app.secret_key = 'kable'

class Asd(FlaskForm):
    name = StringField('Name')
    l_name = StringField('ForName')
    test = StringField('Test')
    submit = SubmitField('Submit')

@app.route('/', methods=['GET', 'POST'])
def test():
    test = False
    form = Asd()

    if request.method == "POST":
        if request.form.action == "one":
            test = form.name.data
            return redirect(url_for('sub', form=form, test=test))

        elif request.form.action == "two":
            test = form.l_name.data
            return redirect(url_for('sub', form=form, test=test))

    elif request.method =='GET':
        return render_template('home.html', form=form)

@app.route('/test', methods=['GET', 'POST'])
def sub(test):
    form = Asd()
    if form.validate_on_submit():
        if request.form['submit'] == "one":
            form.test.data = test
            # send_test = form.name.data

            return redirect(url_for('sub'))

        elif request.form['submit'] == "two":
            form.test.data = test
            # send_test = form.l_name.data

            return redirect(url_for('sub'))

    elif request.method =='GET':
        return render_template('home.html', form=form)



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

html

    {{ form.hidden_tag() }}
    {{ form.name.label }}
    {{ form.name(class="form-group") }}
    <!-- {{ form.submit(value='one') }} -->
    <div class="form-group">
        <input type ="submit" name="action" value="one">
    </div>
    <br>
    {{ form.l_name.label }}
    {{ form.l_name(class="form-group") }}
    <!-- {{ form.submit(value='two') }} -->
    <div class="form-group">
        <input type ="submit" name="action" value="two">
    </div>
    <br>
    {{ form.test }}
        
</form>
'''

Now it works

from flask import Flask, request, render_template,url_for,session
from flask_wtf import FlaskForm
from werkzeug.utils import redirect
from wtforms import StringField, SubmitField

app = Flask(__name__)

app.secret_key = 'kable'

class Asd(FlaskForm):
    name = StringField('Name')
    l_name = StringField('ForName')
    test = StringField('Test')
    submit = SubmitField('Submit')

@app.route('/', methods=['GET', 'POST'])
def test():
    # test = False
    form = Asd()

    if request.method == "POST":
        if request.form["action"] == "one":
            session['name'] = form.name.data
            session['l_name'] = form.l_name.data
            session['test'] = form.name.data
            return redirect(url_for('sub'))

        elif request.form["action"] == "two":
            session['name'] = form.name.data
            session['l_name'] = form.l_name.data
            session['test'] = form.l_name.data
            return redirect(url_for('sub'))

    elif request.method =='GET':
        return render_template('home.html', form=form)

@app.route('/test', methods=['GET', 'POST'])
def sub():
    form = Asd()
    if request.method == "POST":
        if request.form["action"] == "one":
            session['name'] = form.name.data
            session['l_name'] = form.l_name.data
            session['test'] = form.name.data
            return redirect(url_for('sub'))

        elif request.form["action"] == "two":
            session['name'] = form.name.data
            session['l_name'] = form.l_name.data
            session['test'] = form.l_name.data
            return redirect(url_for('sub'))

    elif request.method =='GET':

        form.name.data = session['name']
        form.l_name.data = session['l_name']
        form.test.data = session['test']
    return render_template('home.html', form=form)



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

'''

    {{ form.hidden_tag() }}
    {{ form.name.label }}
    {{ form.name(class="form-group") }}
    <!-- {{ form.submit(value='one') }} -->
    <div class="form-group">
        <input type ="submit" name="action" value="one">
        <!-- {{ form.submit(class="btn", value="one") }} -->
    </div>
    <br>
    {{ form.l_name.label }}
    {{ form.l_name(class="form-group") }}
    <!-- {{ form.submit(value='two') }} -->
    <div class="form-group">
        <input type ="submit" name="action" value="two">
        <!-- {{ form.submit(class="btn", value="two") }} -->
    </div>
    <br>
    {{ form.test }}
        
</form>
question from:https://stackoverflow.com/questions/65938462/multiple-submit-buttons-in-flask

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should get the input value on POST requests with:

if request.form['action'] == "one":

or this one to avoid an error

request.form.get('action') == "one":

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...