,可以通过以下步骤实现:
pip install flask
pip install flask-wtf
from flask import Flask, render_template, request
from flask_wtf import FlaskForm
from wtforms import StringField, SubmitField
class Form1(FlaskForm):
field1 = StringField('Field 1')
submit1 = SubmitField('Submit 1')
class Form2(FlaskForm):
field2 = StringField('Field 2')
submit2 = SubmitField('Submit 2')
app = Flask(__name__)
app.config['SECRET_KEY'] = 'your_secret_key'
@app.route('/', methods=['GET', 'POST'])
def index():
form1 = Form1()
form2 = Form2()
if form1.validate_on_submit() and form1.submit1.data:
# 处理表单1的提交逻辑
field1_data = form1.field1.data
# 其他逻辑...
if form2.validate_on_submit() and form2.submit2.data:
# 处理表单2的提交逻辑
field2_data = form2.field2.data
# 其他逻辑...
return render_template('index.html', form1=form1, form2=form2)
<form method="POST" action="/">
{{ form1.hidden_tag() }}
{{ form1.field1.label }} {{ form1.field1 }}
{{ form1.submit1 }}
</form>
<form method="POST" action="/">
{{ form2.hidden_tag() }}
{{ form2.field2.label }} {{ form2.field2 }}
{{ form2.submit2 }}
</form>
通过以上步骤,你可以在Flask应用程序中使用flask-wtforms中的多个表单,并将数据传递给它们。请注意,这只是一个基本示例,你可以根据实际需求进行修改和扩展。
领取专属 10元无门槛券
手把手带您无忧上云