我是flask和 的新手Javascript。我想要做的就是创建一个带有动态下拉列表的表单,获取选择的值并将它们放入数据库中。
现在我处于创建动态下拉列表的第一阶段。
这是代码的我的python部分。
class Form(FlaskForm):
site = SelectField('Site', choices=[(' ', ' '), ('International', 'International'), ('Domestic', 'Domestic')])
location = SelectField('Location', choices=[])
city = SelectField('City', choices=[])
street = SelectField('Street', choices=[])
sub_street = SelectField('BuildingStreet', choices=[])
floor = SelectField('Floor', choices=[])
room = SelectField('Room', choices=[])
side = SelectField('Side', choices=[])
@app.route('/')
def basic():
return render_template('basic.html')
@app.route('/Welcome', methods=['GET', 'POST'])
def index():
form = Form()
location_choice_list = [(locations.id, locations.location) for locations in
Hostname.query.filter_by(site='International').all()]
form.location.choices = remove_duplicates.unique_list(location_choice_list)
city_choice_list = [(cities.id, cities.city) for cities in Hostname.query.filter_by(site='International').all()]
form.city.choices = remove_duplicates.unique_list(city_choice_list)
street_choice_list = [(streets.id, streets.street) for streets in
Hostname.query.filter_by(site='International').all()]
form.street.choices = remove_duplicates.unique_list(street_choice_list)
sub_street_choice_list = [(sub_streets.id, sub_streets.sub_street) for sub_streets in
Hostname.query.filter_by(site='International').all()]
form.sub_street.choices = remove_duplicates.unique_list(sub_street_choice_list)
所以首先我创建了一个类,其中包含用户可以从表单中选择的所有下拉列表。除了站点之外,所有其他选项最初都填充有对应于“国际”的选项。即“国际”对应的所有地点,“国际”对应的所有城市等等。我使用了一个小函数作为模块来删除数据库中无法避免的重复条目。最初,当显示表单时,一切都按预期进行。我没有放置 basic.html 文件,因为它只包含一个指向“/Welcome”的链接。此外,我用作模块的 remove_duplicates 函数也没有问题。
拉风的咖菲猫
相关分类