我以相同的方法验证三种形式,我能够验证三种形式中的两种,但是第三种形式没有验证。
views.py
......
@main.route('/tournament/<tid>', methods=["GET", "POST"])
def tournament(tid):
......
form1 = AddTeamForm()
form2 = CreateTeamForm()
form3 = CreateMatchForm()
# Dynamic allocation of team choices
team_choices = [(team.id, team.name) for team in tournament.teams]
form3.team1.choices = team_choices
form3.team2.choices = team_choices
if form1.validate_on_submit():
.....
return redirect(url_for('main.tournament', tid=tid))
if form2.validate_on_submit():
.....
return redirect(url_for('main.tournament', tid=tid))
if form3.validate_on_submit():
print('Passed the validation')
team1 = Team.query.get(int(form3.team1.data))
team2 = Team.query.get(int(form3.team2.data))
match = Match(
tournment_id=tid,
name=team1.name+' Vs '+team2.name,
match_date=form3.match_date.data)
match.teams.append(team1)
match.teams.append(team2)
db.session.add(match)
db.session.commit()
return redirect(url_for('main.tournament', tid=tid))
return render_template(
'main/tournment.html',
form1=form1,
form2=form2,
form3=form3)
forms.py
....
class CreateMatchForm(FlaskForm):
team1 = SelectField('Team1', coerce=int, validators=[DataRequired()])
team2 = SelectField('Team2', coerce=int, validators=[DataRequired()])
match_date = DateField('Match Date', validators=[DataRequired()],
render_kw={'type': 'date'},
default=date.today()+timedelta(days=1))
submit = SubmitField('Create New Match')
tournment.html
....
<form class="form-inline" method="POST" action="{{ url_for('main.tournament', tid=tid) }}">
{{ form3.hidden_tag() }}
{{ form3.team1(class="form-control m-2")}}
{{ form3.team1(class="form-control m-2")}}
{{ form3.match_date(class="form-control m-2")}}
{{ form3.submit(class="form-control m-2 btn btn-success btn-sm")}}
</form>
这是用户界面的外观:
发布请求后:
甚至都没有打印打印/错误消息。
第三种形式不验证任何值,因此不创建匹配项。我试图在烧瓶外壳中使用相同的脚本创建匹配项,它可以正常工作并且正在创建匹配项,但是无法使用此表单创建。我如何通过它?
我想出了一个临时解决方案。我没有使用烧瓶形式,而是使用了原始的html形式,并且可以使用。