简体   繁体   English

带有2种形式的flask-wtf行为

[英]flask-wtf behavior with 2 forms in view

example 2 forms in one page: 一页中的示例2表格:

<form name="form_A" class="form-inline" action="/" method="post">
{{form_A.hidden_tag()}}
    {{form_A.name(type="text", class="form-control")}}
    <input type="submit" name="form_A" id="form_A" class="btn btn-sm btn-success" value="FORM_A">
</form>

<form name="form_B" class="form-inline" action="/" method="post">
{{form_B.hidden_tag()}}
    {{form_B.name(type="text", class="form-control")}}
    <input type="submit" name="form_A" id="form_A" class="btn btn-sm btn-success" value="FORM_B">
</form>

and router: 和路由器:

@app.route('/', methods = ['GET', 'POST'])
def index():
    form_A = ExampleForm1(request.form)
    form_B = ExampleForm1(request.form)
    if form_A.validate_on_submit():
        ...
    if form_B.validate_on_submit():
        ...

any submit button (FORM_A or B) call form_A.validate_on_submit() and form_B.validate_on_submit() together(!). 任何提交按钮(FORM_A或B)都一起调用form_A.validate_on_submit()和form_B.validate_on_submit()。 why? 为什么?
and how to submit only one form in this case? 在这种情况下如何只提交一种表格?

EDIT: 编辑:

i solve this problem just add check data any field and use is_submitted method: 我解决了这个问题,只需在任何字段中添加检查数据并使用is_submitted方法:

if form_A.is_submittied() and form_A.name.data:
    ...

When using more than one form, you need to give each form a prefix: 如果使用多种形式,则需要给每种形式加上前缀:

form_A = ExampleForm1(request.form, prefix='form_a')
form_B = ExampleForm1(request.form, prefix='form_b')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM