两个wtforms在同一页面上提交。 validate_on_submit仅适用于last

时间:2014-04-17 17:00:11

标签: python flask wtforms

我有两个不同的wtforms,在一个页面上有“submit”按钮。 单击其中任何一个都会激活最后一个,因此validate_on_submit函数始终为第二个按钮返回True。

forms.py

class SearchByAuthorForm(Form):
    author_name = TextField('author_name', validators = [Length(max=25)])
    author_surname = TextField('author_surname',validators = [Length(max=25)])
    submit = SubmitField("Search")


class SearchByBookForm(Form):
    title = TextField('title', validators = [Length(max=50)])
    submit = SubmitField("Search")

views.py

@app.route('/',  methods = ['GET','POST'])
@app.route('/index', methods = ['GET','POST'])
@login_required
def index():
    user = g.user 
    aform = SearchByAuthorForm(request.form)
    bform = SearchByBookForm(request.form)
    search_result = []
    search_type = ""
    print aform.errors
    print bform.errors
    if aform.validate_on_submit():
        author_name = aform.author_name.data.lower()
        author_surname = aform.author_surname.data.lower()
        search_type = "author"
        if author_name != "" and author_surname == "":
            search_result = Author.query.filter_by(author_name = author_name).all()
        if author_name == "" and author_surname != "":
            search_result = Author.query.filter_by(author_surname = author_surname).all()
        if author_name != "" and author_surname != "":
            search_result = Author.query.filter_by(author_name = author_name, author_surname = author_surname).all()
    if bform.validate_on_submit():
        title = bform.title.data.lower()
        search_result = Book.query.filter_by(title = title).all()
        search_type = "book"
    print aform.errors
    print bform.errors
    return render_template('index.html', user = user, a_form = aform, b_form = bform, search_result = search_result, search_type = search_type)

模板:

<div style="width:100%">
<div style="width:50%;float:left;">
<form action="" method="post" name="author_search">
    {{ a_form.hidden_tag() }}
        <h2>Search by author</h2><br>
        <table>
        <tr>
        <td>Author's Name:</td><td>{{ a_form.author_name(size=25) }}</td>
        </tr>
        <tr>
        <td>Author's Surname:</td><td>{{ a_form.author_surname(size=25) }}<td>
        </tr>
        </table>
        <p>{{ a_form.submit }}</p>
</form>
</div>
<div style="width:50%;float:left;">
<form action="" method="post" name="book_search">
    {{ b_form.hidden_tag() }}
        <h2>Search by book</h2><br>
        <table>
        <tr>
        <td>Title:</td><td>{{ b_form.title(size=25) }}</td>
        </tr>
        </table>
    <p><input type="submit" value="Search" name="book"></p>
</form>
</div>
</div>

1 个答案:

答案 0 :(得分:0)

这个问题很难回答,因为它缺乏背景。你究竟想做什么?您似乎要做的是将表单子类化。在这种情况下,这是一个example

class ProfileForm(Form):
    birthday  = DateTimeField('Your Birthday', format='%m/%d/%y')
    signature = TextAreaField('Forum Signature')

class AdminProfileForm(ProfileForm):
    username = StringField('Username', [validators.Length(max=40)])
    level = IntegerField('User Level', [validators.NumberRange(min=0, max=10)])

这里发生的是AdminProfileForm假设ProfileForm的值。调用一个AdminProfileForm然后为两者生成所有字段。