Pergunta

I have two different wtforms with "submit" button on one page. Clicking on any of them activates the last, so validate_on_submit function always return True for the 2nd button.

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)

template:

<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>
Foi útil?

Solução

This question is hard to answer because it lacks context. What exactly are you trying to do? It appears what you are trying to do is subclass a form. In that case, here is an 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)])

What's happening here is the AdminProfileForm is assuming the values of ProfileForm. Calling one AdminProfileForm then generates all the fields for both.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top