我正在以相同的方法验证三种形式,我能够验证三种形式中的两种,但是第三种形式没有验证。
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>
这是用户界面的外观:
发布请求后:
它甚至都没有打印出打印/错误消息。
第三种形式不验证任何值,因此不创建匹配项。我试图在烧瓶外壳中使用相同的脚本创建匹配项,它可以正常工作并且正在创建匹配项,但是无法使用此表单创建。我该怎么办?
答案 0 :(得分:0)
我想出了一个临时解决方案。我没有使用烧瓶形式,而是使用了原始的html形式,并且有效。
在views.py中将form3替换为请求对象:
if request.method == "POST":
print('Passed the validation')
team1 = Team.query.get(int(request.form['team1']))
team2 = Team.query.get(int(request.form['team2']))
match = Match(
tournament_id=tid,
name=team1.name+' Vs '+team2.name,
match_date=request.form['match_date'])
match.teams.append(team1)
match.teams.append(team2)
db.session.add(match)
db.session.commit()
return redirect(url_for('main.tournament', tid=tid))
用原始html形式替换烧瓶形式:
<form class="form-inline" method="POST" action="{{ url_for('main.tournment', tid=tid) }}">
<select class="form-control m-2" id="team1" name="team1" required="">
{% for team in tournament.teams %}
<option value={{team.id}}>{{team.name}}</option>
{% endfor %}
</select>
<select class="form-control m-2" id="team2" name="team2" required="">
{% for team in tournament.teams %}
<option value={{team.id}}>{{team.name}}</option>
{% endfor %}
</select>
<input class="form-control m-2" type="date" name="match_date" required="">
<input class="form-control m-2 btn btn-success btn-sm" type="submit" value="Create Match">
</form>
现在它可以正常工作了:
还是,我一无所知。为什么不能使用烧瓶形式工作?