Skip to content Skip to sidebar Skip to footer

Listing Table Results To Html With Flask

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

Solution 1:

Solved it with this code, much cleaner too!

@app.route('/view_answered/', methods=['GET','POST'])defview_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], 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 %}

Post a Comment for "Listing Table Results To Html With Flask"