我创建的从瓶窗体继承的窗体仅在站点上接收数据窗体的第一个窗体字段。
我尝试了表单字段顺序的不同组合,并在表单中添加了额外的字段以确保100%确定。
loginForm:
class LoginForm(FlaskForm):
username = StringField('Username',
validators=[DataRequired()])
password = PasswordField('Password',
validators=[DataRequired()])
submit = SubmitField('Login')
otherField = StringField('testField')
login.html的定义方式
<div class="content-section">
<form method="POST" action="">
{{ form.hidden_tag() }}
<fieldset class="form-group">
<legend class="border-bottom mb-4">Join Today</legend>
<form class="form-group">
{{ form.otherField.label(class="form-control") }}
{{ form.otherField(class="form-control") }}
</form>
<form class="form-group">
{{ form.username.label(class="form-control") }}
{{ form.username(class="form-control") }}
</form>
<form class="form-group">
{{ form.password.label(class="form-control") }}
{{ form.password(class="form-control") }}
</form>
<div class="form-group">
{{ form.submit(class="btn btn-outline-info") }}
</div>
</fieldset>
</form>
</div>
页面源的外观
<div class="content-section">
<form method="POST" action="">
<input id="csrf_token" name="csrf_token" type="hidden" value="tag value was removed- nothing to do with case">
<fieldset class="form-group">
<legend class="border-bottom mb-4">Join Today</legend>
<form class="form-group">
<label class="form-control" for="otherField">testField</label>
<input class="form-control" id="otherField" name="otherField" type="text" value="">
</form>
<form class="form-group">
<label class="form-control" for="username">Username</label>
<input class="form-control" id="username" name="username" required type="text" value="">
</form>
<form class="form-group">
<label class="form-control" for="password">Password</label>
<input class="form-control" id="password" name="password" required type="password" value="">
</form>
<div class="form-group">
<input class="btn btn-outline-info" id="submit" name="submit" type="submit" value="Login">
</div>
</fieldset>
</form>
</div>
@app.route("/login", methods=['POST','GET'])
def login():
form = LoginForm()
if request.method == 'POST':
if form.is_submitted():
user = Specialist.query.filter_by(username=form.username.data).first()
if user and form.password.data == user.password:
return 'login'
else:
return 'didn't login'
return render_template("login.html", form=form)
因此,在调试控制台中,我检查了以下值: form.otherField.data,它具有我输入形式的值。 form.password.data和form.username.data具有“”值。
当我更改了表单字段的顺序以使form.username首次出现在站点上时,route函数从该表单接收了数据,但是其他表单字段具有“值”。