0
我正在使用Flask和WTForm來製作一個網絡應用程序,到目前爲止我的其他兩個字段驗證正常,但SelectField拋出錯誤'請選擇一個寵物'。正如我自己指定的那樣,即它認爲它沒有收到有效的輸入。 SelectField.choices匹配到呈現爲HTML的選項,所以我做錯了什麼?Flask針對SelectField的WTForm驗證失敗...爲什麼?
app.py
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)
index.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 %}
編輯我在神社文件,productF更新的代碼字段的名字,我仍然收到錯誤。 Pets
對象作爲JSON對象而不是app.py加載到HTML頁面中,但據我所知,這些值是相同的;這是怎樣的選擇元素來自檢查者出現:
<select id="product" class="form-control" name="product">
<option value="-1">-----</option>
<option value="0">Cat</option>
<option value="1">Dog</option>
<option value="2">Parrot</option>
<option value="3">Hamster</option>
</select>
將它改爲'choices = pChoices'後仍然拋出同樣的錯誤 –
請參閱編輯答案:) –
仍然沒有運氣,請參閱我編輯的問題 –