flask - Display database from python to html

后端 未结 3 1380
既然无缘
既然无缘 2021-02-01 09:40

I have code like this to retrieve data from database and I want to display it in html.

This is app.py

@app.route(\'/news\')
def news():
         


        
3条回答
  •  时光说笑
    2021-02-01 09:41

    You can pass your data using render_template() like this:

    cur = con.cursor()
    cur.execute("SELECT * FROM dataset")
    data = cur.fetchall()
    render_template('template.html', data=data)
    

    Then in your template, iterate over the rows, for example you could render table rows for each row:

    {% for item in data %}
    
        {{item[0]}}
        {{item[1]}}
        ...
    
    {% endfor %}
    

提交回复
热议问题