我有一个包含5个字段列表的网页。我正在尝试根据用户选择的字段生成输出。
这是我当前可以正常使用的代码。
fields = {field: value for field, value in form.data.items() if
value != None or value != 'csrf_token'}
print(fields)
## This prints out a list of all fields
o = []
for a in fields:
if a is not None:
o.append(a)
print(o)
p = o[:-1]
print(p)
output = sample_function(*p)
我遇到的问题是无论选择哪个字段,输出都是相同的(无论是否选择它们,它都会返回所有字段)。
任何人都可以告诉我我在哪里出错了。谢谢。
更新:
@app.route('/index', methods=['GET','POST'])
def sample_function():
form = SampleForm()
if form.validate_on_submit():
store_id = form.store_id.data
store_name = form.store_name.data
location_id = form.location_id.data
store_type = form.store_type.data
store_location = form.store_location.data
fields = {field: value for field, value in form.data.items() if
value != None or value != 'csrf_token'}
print(fields)
## This prints out a list of all fields
o = []
for a in fields:
if a is not None:
o.append(a)
print(o)
p = o[:-1]
print(p)
output = sample_function(*p)
运行查询的函数:
def sample_function(*field_names):
cursor = conn.cursor()
cursor.execute('select {} from table.format(', '.join(str(field) for field in field_names)))