返回所有字段而不是所选字段

2024-04-25 13:30:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个网页与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)))

Tags: samplestoreinformnoneidfieldfields