我尝试创建将填充SelectField:EVENT_NAME的函数,但问题是当我想将列表发送到SelectField时。你知道我做错了吗?
我创建了函数 autocompleteEvent ,它询问db并将结果作为列表返回。
@app.route('/autocompleteEvent',methods=['GET'])
def autocompleteEvent():
today = time.strftime("%m/%d/%Y")
json_list = []
name=current_user.USERNAME
q = USER.query.filter_by(USERNAME=name).first()
ID_FEDERATION=q.FEDERATION.ID_FEDERATION
q = EVENTS.query.filter_by(ID_FEDERATION=ID_FEDERATION).all()
for i in q:
if i.DATE>=today:
json_list.append(i.EVENT_NAME)
else:
continue
print(json_list)
search = request.args.get('term')
app.logger.debug(search)
return jsonify(json_list=json_list)
结果我得到了:
{
"json_list": [
"UFC 205",
"UFC 206"
]
}
Flask Form:
json_lists = []
class FIGHTS(FlaskForm):
EVENT_NAME = SelectField(u'Choose eventname',id='EVENT_NAME',choices=json_lists[])
jQuery代码:
<script type="text/javascript">
$(function() {
$.ajax({
url: '{{ url_for("autocompleteEvent") }}'
}).done(function (data) {
$('EVENT_NAME').autocomplete({
$.each(json_list, function(index, value) {
('EVENT_NAME').append($('<option>').text(json_list).attr('value', index));
});
});
});
</script>