使用Flask将表格结果列出为HTML [英] Listing table results to HTML with Flask
本文介绍了使用Flask将表格结果列出为HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在努力将我的SELECT all语句结果传递给我的视图.我正在使用flask和MySQLdb.将我的表中的所有结果显示到HTML页面的正确语法是什么?我很想通过一个列表,但是无法弄清楚它的语法.
I am struggling with passing my SELECT all statement results to my view. I am using flask and MySQLdb. What is the proper syntax to display all my results from my table to my HTML page? I would love to pass a list through, but was unable to figure the syntax out for that.
HTML调用{{ session.qid }}
仅用于测试,没有显示任何内容.
HTML call {{ session.qid }}
just to test, and nothing displayed.
这是我到目前为止拥有的python代码:
@app.route('/view_answered/', methods=['GET','POST'])
def view_answered():
error = ''
try:
if request.method == 'POST':
c, conn = connection()
clientcid = session['clientcid']
cursor.execute("SELECT * FROM tickets WHERE cid = 1 AND solved = 0")
results = cursor.fetchall()
x = 0
qid = []
difficulty = []
time_stamp = []
title = []
body = []
for row in results:
qid[x] = row[0]
difficulty[x] = row[3]
time_stamp[x] = row[4]
title[x] = row[6]
body[x] = row[7]
x = x + 1
conn.commit()
c.close()
conn.close()
gc.collect()
session['qid'] = qid
session['difficulty'] = difficulty
session['time_stamp'] = time_stamp
session['title'] = title
session['body'] = body
return redirect(url_for('view_answered'))
else:
error = "Something is aloof."
return render_template("view_answered.html")
except Exception as e:
return(str(e))
推荐答案
使用此代码解决了它,也更加干净!
Solved it with this code, much cleaner too!
@app.route('/view_answered/', methods=['GET','POST'])
def view_answered():
error = ''
try:
result = ''
c, conn = connection()
clientcid = session['clientcid']
c.execute("SELECT * FROM tickets WHERE cid = (%s) AND solved = 1", (clientcid,))
result = c.fetchall()
conn.commit()
c.close()
conn.close()
return render_template("view_unanswered.html", result = result)
except Exception as e:
return(str(e))
VIEW (r [0],r [2]等,对应于SQL表中的列位置)
VIEW (r[0], r[2], etc, corresponding to column position in SQL table)
{% for r in result %}
<li>Question ID: {{ r[0] }}</li>
<li>Difficulty: {{ r[3] }}</li>
<li>Time Submitted: {{ r[4] }}</li>
<li>Title: {{ r[6] }}</li>
<li>Body: {{ r[7] }}</li>
<li>Answer: {{ r[8] }}</li>
<li>By: {{ r[3] }}</li>
<br><br>
{% endfor %}
这篇关于使用Flask将表格结果列出为HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文