我正在使用Flask和WTForm制作一个网络应用程序,到目前为止,我的其他两个字段验证正常,但SelectField正在抛出错误“请选择宠物”。由我自己指定,即它认为它没有收到有效的输入。 SelectField.choices匹配呈现为HTML的选项,所以我做错了什么?
app.py
yAxis
index.jinja
from flask_wtf import Form
from flask_wtf.csrf import CSRFProtect
from wtforms import IntegerField, StringField, SelectField, validators,
ValidationError
from flask import Flask, request, flash, render_template
app = Flask(__name__)
app.secret_key = 'xxxxxxxxxxxx'
csrf = CSRFProtect(app)
pChoices = [('-1', '-----'),
('0', 'Cat'),
('1', 'Dog'),
('2', 'Parrot'),
('3', 'Hamster')]
class paymentForm(Form):
productF = SelectField('productF', [validators.input_required("Please choose a pet.")], choices=[pChoices], coerce=int)
buyer_id = StringField('buyer', [validators.input_required("Please enter your name.")])
paid = DecimalField('paid', [validators.input_required("please enter payment amount")])
@app.route('/', methods=['POST', 'GET'])
def index():
form = paymentForm(request.form, csrf_enabled=True)
if form.validate_on_submit():
flash('Order Placed Successfully', 'success')
data = form.data
record_order(data)
print(data['price'])
return render_template('confirmation.jinja',
pets=PETS,
title='Confirmation Page',
form=form, error=form.errors,
** context)
elif not form.validate_on_submit:
flash('Submission error, please check the form', 'success')
return render_template('index.jinja',
title='Order Form',
form=form,
error=form.errors,
**context)
if __name__ == '__main__':
app.run(debug=True, use_reloader=True)
编辑我已经使用productF更新了代码作为jinja文件中的字段名称,但仍然收到错误。 {% extends 'base.jinja' %}
{% block main %}
<form method="post">
<div class="form-group row">
<label for="product" class="col-sm-2 form-control-label">Pet:</label>
<div class="col-sm-8">
<select id="productF" class="form-control" name="pet" onchange="Update()">
<option value="-1">-----</option>
{% for pet in pets %}
<option value="{{ loop.index -1 }}">{{ pet.name }}</option>
{% endfor %}
</select>
</div>
</div>
<div class="form-group row">
<label for="buyer" class="col-sm-2 form-control-label">Buyer:</label>
{% for message in form.buyer_id.errors %}
<div>{{ message }}</div>
{% endfor %}
<div class="col-sm-10">
<input type="text" class="form-control" id="buyer_id" name="buyer_id" placeholder="Buyer" value="{{ buyer_id }}" required>
</div>
</div>
<div class="form-group row">
<label for="paid" class="col-sm-2 form-control-label">Amount Paid:</label>
{% for message in form.paid.errors %}
<div>{{ message }}</div>
{% endfor %}
<div class="col-sm-10">
<input type="text" class="form-control" id="paid" name="paid" placeholder="Amount Paid" value="{{ paid }}" required>
</div>
</div>
{% if form.errors %}
{{ form.errors }}
{% endif %}
<div class="form-group row">
<div class="col-sm-offset-2 col-sm-10">
<button type="submit" class="btn btn-primary">Place Order</button>
</div>
</div>
<input type="hidden" name="csrf_token" value="{{ csrf_token()
}}"/>
</form>
{% endblock %}
对象作为JSON对象而不是app.py加载到HTML页面中,但据我所知,值是相同的;这是选择元素从检查器中显示的方式:
Pets
答案 0 :(得分:1)
您将pChoices
定义为列表
pChoices = [('-1', '-----'),
('0', 'Cat'),
('1', 'Dog'),
('2', 'Parrot'),
('3', 'Hamster')]
然后将其包装在SelectField
定义中的另一个列表中:
productF = SelectField(..., choices=[pChoices], ...)
使用choices=pChoices
应该修复它。
修改:您还可以使用pet
作为jinja模板中的字段名称,您可能希望使用productF
。
编辑2 :您在定义pChoices
的值和字段强制的方式上存在不匹配。从表单字段中删除coerce=int
或将pChoices
中元组的第一个值设置为整数,而不是字符串。