【发布时间】:2020-11-02 06:54:00
【问题描述】:
我正在创建一个关于考试系统的项目。 在这里,我有一个名为 questionpapers 的数据库 (db3),其中包含 6 个表格形式的试卷。 我想在我的网页中以超链接的形式返回这些论文的名称。
这是我的python代码:
@app.route('/qsetdisplay',methods=['GET','POST'])
def qset_display():
if request.method == 'GET':
print("found template")
cursor4 = db3.cursor(pymysql.cursors.DictCursor)
cursor4.execute("show tables")
papers = cursor4.fetchall()
print("papers:",papers)
names = []
for p in papers:
print("for p in papers:")
print("p:",p)
value = list(p.values())
print("value:",value)
names.append(value)
print("names:",names)
l = len(names)
for i in range(l):
print(names[i][0])
return(names[i][0])
return "THESE ARE THE AVAILABLE PAPERS"
return render_template('profile.html')
if __name__ == '__main__':
app.run(debug=True)
return 语句只返回第一个元素并结束循环。
这是我的编译器输出:
found template
papers: [{'Tables_in_questionpapers': 'pcdata'}, {'Tables_in_questionpapers': 'physics1'}, {'Tables_in_questionpapers': 'physics_test'}, {'Tables_in_questionpapers': 'sample'}, {'Tables_in_questionpapers': 'sample1'}, {'Tables_in_questionpapers': 'samplea'}]
for p in papers:
p: {'Tables_in_questionpapers': 'pcdata'}
value: ['pcdata']
for p in papers:
p: {'Tables_in_questionpapers': 'physics1'}
value: ['physics1']
for p in papers:
p: {'Tables_in_questionpapers': 'physics_test'}
value: ['physics_test']
for p in papers:
p: {'Tables_in_questionpapers': 'sample'}
value: ['sample']
for p in papers:
p: {'Tables_in_questionpapers': 'sample1'}
value: ['sample1']
for p in papers:
p: {'Tables_in_questionpapers': 'samplea'}
value: ['samplea']
names: [['pcdata'], ['physics1'], ['physics_test'], ['sample'], ['sample1'], ['samplea']]
pcdata
我尝试了收益,但没有成功。 我正在使用 flask 和 pymysql 包。
我怎样才能crct这个代码?
还有没有办法以超链接的形式返回元素?
我想在我的网页中返回所有6个试卷名称
【问题讨论】:
-
嗯,这就是
return所做的——它完成执行并返回值。你想得到什么作为代码的输出?