如何在同一个模板中添加2个工作表单?

时间:2019-02-01 05:20:16

标签: python session flask flask-wtforms

我需要在同一网页上添加2个单独的表单,但我无法获得第二个表单来输出任何信息。

在我的研究中,我看到有人建议将表格分为2个不同的def函数,但我在弄清楚如何使两个表格在合理的时间保持可用方面遇到困难。

from flask import Flask, session, render_template, url_for, redirect
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField


app = Flask(__name__)
app.config['SECRET_KEY'] = 'b317a06ad972917a84be4c6c14c64882'


class PostForm(FlaskForm):
    content = StringField('Content')
    submit = SubmitField('Submit')


class SecondPostForm(FlaskForm):
    content = StringField('Second Content')
    submit = SubmitField('Second Submit')


@app.route("/", methods=['GET', 'POST'])
@app.route("/home", methods=['GET', 'POST'])
def home():
    form = PostForm()
    second_form = SecondPostForm()
    if form.validate_on_submit():
        print(form.content.data)
        session['content'] = form.content.data
        redirect(url_for('submit'))
        return redirect(url_for('submit'))
'''
--------------------------------------------------------------------
is it possible to split the second if statement onto its own def and keep 
them both usable on the same page at the same time?
--------------------------------------------------------------------

'''
    elif second_form.validate_on_submit():
        print(second_form.content.data)
        session['content'] = second_form.content.data
        return redirect(url_for('othersubmit'))
    return render_template('example.html', second_form=second_form, form=form)


@app.route("/submit", methods=['GET', 'POST'])
def submit():
    content = session.get('content', None)
    print(content)
    session.pop('content', None)
    return redirect(url_for('home'))


@app.route("/othersubmit", methods=['GET', 'POST'])
def othersubmit():
    print('othersubmit')
    content = session.get('content', None)
    print(content)
    session.pop('content', None)
    return redirect(url_for('home'))


if __name__ == "__main__":
    app.run(debug=True)





<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<div class="content-section">
    <form method="POST" action="">
        {{ form.hidden_tag() }}
        <fieldset class="form-group">
            <legend class="border-bottom mb-4">{{ legend }}</legend>
            <div class="form-group">
                {{ form.content.label(class="form-control-label") }}
                {% if form.content.errors %}
                    {{ form.content(class="form-control form-control-lg is-invalid") }}
                    <div class="invalid-feedback">
                        {% for error in form.content.errors %}
                            <span>{{ error }}</span>
                        {% endfor %}
                    </div>
                {% else %}
                    {{ form.content(class="form-control form-control-lg") }}
                {% endif %}
            </div>
        </fieldset>
        <div class="form-group">
            {{ form.submit(class="btn btn-outline-info") }}
        </div>
    </form>
    <form method="POST" action="{{ url_for('othersubmit') }}">
        {{ second_form.hidden_tag() }}
        <fieldset class="form-group">
            <legend class="border-bottom mb-4">{{ legend }}</legend>
            <div class="form-group">
                {{ second_form.content.label(class="form-control-label") }}
                {% if second_form.content.errors %}
                    {{ second_form.content(class="form-control form-control-lg is-invalid") }}
                    <div class="invalid-feedback">
                        {% for error in second_form.content.errors %}
                            <span>{{ error }}</span>
                        {% endfor %}
                    </div>
                {% else %}
                    {{ second_form.content(class="form-control form-control-lg") }}
                {% endif %}
            </div>
        </fieldset>
        <div class="form-group">
            {{ second_form.submit(class="btn btn-outline-info") }}
        </div>
    </form>
</div>
</body>
</html>

我尝试了是否使用action =“ {{url_for('othersubmit')}}”“都没有达到预期的效果

目标是让表格打印自己的数据并打印其来源。目前,第一种表格打印两次相同的数据,而第二种表格什么也不打印。

1 个答案:

答案 0 :(得分:0)

您可以执行以下操作: 1)首先,如果一页上有2个表单,则不能有两个{{second_form.hidden_​​tag()}},因为它们会引发具有相同ID的错误。根据wtform文档,我已将其作为{{form.csrf_token(id ='login_csrf')}}处理。看到您可以指定自己的ID,这样可以防止冲突。

2)在两个表格中都更改Submit按钮的名称。使它们与众不同。然后,您可以执行以下操作。如您所见,我在同一页面上有2个表单form_login和form_reg。我一直保持提交按钮名称的差异。一个是登录名,另一个是注册名。

if form_login.login.data:
        if form_login.validate_on_submit():
            #do something here

    elif form_reg.register.data:
        if form_reg.validate_on_submit():
            #do something here

    return render_template('abc.html')

这应该对您有用。