【问题标题】:Two wtforms submit on the same page. validate_on_submit works only with last两个 wtform 在同一页面上提交。 validate_on_submit 仅适用于 last
【发布时间】:2014-04-17 17:00:10
【问题描述】:

我有两个不同的 wtforms,一页上有“提交”按钮。 单击其中任何一个都会激活最后一个,因此 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>

【问题讨论】:

    标签: python flask wtforms


    【解决方案1】:

    这个问题很难回答,因为它缺乏上下文。你到底想做什么?看来您要做的是子类化表单。在这种情况下,这里是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 然后为两者生成所有字段。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-14
      • 2017-03-21
      • 1970-01-01
      • 2016-06-25
      • 2012-11-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多