Flask 中的多个提交按钮

Posted

技术标签:

【中文标题】Flask 中的多个提交按钮【英文标题】:Multiple submit Buttons in Flask 【发布时间】:2021-05-02 10:16:04 【问题描述】:

我尝试了这个问题的方法: use many submit buttons in the same form

当我尝试这种方法时,我得到了:

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

谁能告诉我我做错了什么? '''Python 从烧瓶进口烧瓶,请求,render_template,url_for 从 flask_wtf 导入 FlaskForm 从 werkzeug.utils 导入重定向 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>
'''

现在可以使用了

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>

【问题讨论】:

【参考方案1】:

您应该通过以下方式获取 POST 请求的输入值:

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

或者这个来避免错误

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

【讨论】:

以上是关于Flask 中的多个提交按钮的主要内容,如果未能解决你的问题,请参考以下文章

如何检测在单个 Action 类中的多个提交按钮场景中单击的提交按钮? [复制]

Flask 在没有提交按钮的情况下获取复选框值

Flask - 提交按钮值

表格中的多个提交按钮

php一个表单提交多个页面,怎样获取按钮提交过来的值

HTML 表单中的多个提交按钮