在Flask中从MySQL数据库显示HTML上的数据。

huangapple go评论63阅读模式
英文:

Display data on html from mysql database using flask

问题

以下是翻译好的代码部分:

我一直卡在如何使用Flask从MySQL数据库显示数据在HTML上
我的数据库连接成功HTML页面可以显示只是没有显示数据
想要显示数据库中的info_table”。它有两个属性nameage”。
是否正确使用了app.route('/display', methods=['GET'])”?

app.py
display.html:

<!DOCTYPE html>
<html>
<head>
 <title></title>
</head>
<body>
<style>

 td {
        width: 150px;
        text-align: center;
        border: 1px solid black;
        padding: 5px;
      }
</style>
<table>
  <thead>
    <tr>
        <th>name</th>
        <th>age </th>
    </tr>
    </thead>
    <tbody>
     {% for row in data %}
            <tr>
                <td>{{row[0]}}</td>
                <td>{{row[1]}}</td>
            </tr>
        {% endfor %}
    </tbody>
</table>
</body>
</html>
英文:

I been stuck on how to display data on html from mysql database using flask.

My connection to database is successful and html page displays. Just doesn't display with the data

Would like to display the "info_table" from the database. Has 2 attribute "name" and "age"

Is line "app.route('/display', methods=['GET'])" correct?

app.py


from flask import Flask, render_template, request
from flask_mysqldb import MySQL

app = Flask(__name__)
app.debug = True

app.config[&#39;MYSQL_HOST&#39;] = &#39;&#39;
app.config[&#39;MYSQL_USER&#39;] = &#39;root&#39;
app.config[&#39;MYSQL_PASSWORD&#39;] = &#39;&#39;
app.config[&#39;MYSQL_DB&#39;] = &#39;DB&#39;

mysql = MySQL(app)


app.route(&#39;/display&#39;, methods=[&#39;GET&#39;])
def display():
    cursor = mysql.connection.cursor()
    cursor.execute(&quot;SELECT * FROM info_table&quot;)
    data = cursor.fetchall()
    return render_template(&#39;display.html&#39;, info_table=data)


app.run(host=&#39;localhost&#39;, port=3306)

display.html:

t&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
 &lt;title&gt;&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;style&gt;

 td {
        width: 150px;
        text-align: center;
        border: 1px solid black;
        padding: 5px;
      }
&lt;/style&gt;
&lt;table&gt;
  &lt;thead&gt;
    &lt;tr&gt;
        &lt;th&gt;name&lt;/th&gt;
        &lt;th&gt;age &lt;/th&gt;
    &lt;/tr&gt;
    &lt;/thead&gt;
    &lt;tbody&gt;
     {% for row in data %}
            &lt;tr&gt;
                &lt;td&gt;{{row[0]}}&lt;/td&gt;
                &lt;td&gt;{{row[1]}}&lt;/td&gt;
            &lt;/tr&gt;
        {% endfor %}
    &lt;/tbody&gt;
&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;

答案1

得分: 0

首先,我需要感谢你一直在尝试和提问。

关于app.py中的请求处理程序app.route('/display', methods=['GET']),你应该将其定义为装饰器,像这样@app.route('/display', methods=['GET']),然后在display.html中,在for循环中将data变量更改为info_table,因为这是你正在映射数据的真正属性。

英文:

First of all I need to thank you that you're keep trying and asking.

Regarding request handler in app.py app.route(&#39;/display&#39;, methods=[&#39;GET&#39;])
you should define it as a decorator like this @app.route(&#39;/display&#39;, methods=[&#39;GET&#39;])
and then in display.html change the data variable to info_table in the for loop cause this is the real attribute that you're mapping data to it

huangapple
  • 本文由 发表于 2023年4月13日 23:26:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/76007241.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定