gpt4 book ai didi

python - 带有 selectfield 的 flask 链接下拉列表不是有效的选择

转载 作者:太空宇宙 更新时间:2023-11-04 04:16:47 26 4
gpt4 key购买 nike

由于在链式下拉方案中收到“无效选择”错误消息,我被困在当前项目上。我有 2 个下拉字段区域和区域,但仅在该字段上收到错误消息。无论是否选择了字段或使用默认选项。提前致谢

我这样定义类:

class RegistrationDDForm(FlaskForm):
region = SelectField(u'Region', choices=(), coerce=int)
area = SelectField(u'Area', choices=(),coerce=int)
submit = SubmitField('Sign Up')

我的路线和功能定义如下:

@users.route("/finish_registration", methods=['GET', 'POST'])
def finish_registration():
cur = mysql.connection.cursor()
result1 = cur.execute("select * from region")
results = cur.fetchall()

form = RegistrationDDForm(request.form)
form.region.choices = [('0', 'Select Region')] +
[(x['region_id'], x['region']) for x in results]

if form.validate_on_submit():
# Eventually add data to database table
pass

return render_template('finish_registration.html', title='Register', form=form)

@users.route("/finish_registration/<int:region_id>/", methods=["GET"])
def get_request(region_id):

cur = mysql.connection.cursor()
results = cur.execute("select * from area where region_id=%s", region_id])
results = cur.fetchall()

data = [('0', 'Select Area')] +
[(x['area_id'], x['area']) for x in results
if x['region_id'] == region_id]

response = make_response(json.dumps(data))
response.content_type = 'application/json'
return response

我的 javascript 函数:

$(function() {
// disable refresh button
$("#refresh-btn").prop("disabled", true)
$("#area_select").show();

$("#region_select").change(function() {
var region_id = $("#region_select").val();
var get_request = $.ajax({
type: 'GET',
url: '/finish_registration/' + region_id + '/',
});

get_request.done(function(data){
var option_list = data;
$("#area_select").empty();
for (var i = 0; i < option_list.length; i++) {
$("#area_select").append(
$("<option></option>").attr("value", option_list[i][0]).text(option_list[i][1]));
}
});
});

我的 html 页面的部分片段:

<form method="POST" action="">
{{ form.csrf_token }}
<fieldset class="form-group">
<div class="form-group">
{{ form.region.label(class="form-control-label") }}
{{ form.region(id="region_select", class="form-control form-control-sm") }}
</div>
<div class="form-group">
{{ form.area.label(class="form-control-label") }}
{{ form.area(id="area_select", class="form-control form-control-sm") }}
</div>
</fieldset>
<div class="form-group">
{{ form.submit(class="btn btn-outline-info btn-sm btn-block") }}
</div>
</form>

最佳答案

您还必须在验证前填写 form.area.choices。否则没有有效的区域选择...因此如果您的表单提供错误消息,您会收到一条错误消息。

@users.route("/finish_registration", methods=['GET', 'POST'])
def finish_registration():
cur = mysql.connection.cursor()
result1 = cur.execute("select * from region")
results = cur.fetchall()

form = RegistrationDDForm(request.form)
form.region.choices = [('0', 'Select Region')] +
[(x['region_id'], x['region']) for x in results]

if request.method == 'POST':
cur = mysql.connection.cursor()
results = cur.execute("select * from area where region_id=%s", request.form.region_id)
results = cur.fetchall()

# Add choices to area to allow validation
form.area.choices = [(x['area_id'], x['area']) for x in results]

if form.validate():
# Eventually add data to database table
pass

return render_template('finish_registration.html', title='Register', form=form)

关于python - 带有 selectfield 的 flask 链接下拉列表不是有效的选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55252071/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com