用Flas将表格结果列表到HTML

2024-09-29 21:54:28 发布

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

我正在努力将SELECT all语句结果传递给我的视图。我用的是烧瓶和MySQLdb。从表格到HTML页面显示所有结果的正确语法是什么?我很想传递一个列表,但无法找出语法。在

HTML调用{{ session.qid }}只是为了测试,没有显示任何内容。在

以下是我目前掌握的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))

Tags: viewreturntimetitlesessionhtmlstamp语法
1条回答
网友
1楼 · 发布于 2024-09-29 21:54:28

用这个代码解决了它,太干净了!在

@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))

查看(r[0]、r[2]等,对应于SQL表中的列位置)

^{pr2}$

相关问题 更多 >

    热门问题