用于填写MySQL记录的Flask HTML模板

用于填写MySQL记录的Flask HTML模板,mysql,python-2.7,flask,Mysql,Python 2.7,Flask,如何用MySQL数据库中的值在HTML中填写{{}。该应用程序是用Flask编写的,Python版本为2.7.13。烧瓶0.12.2 当浏览到时,我只得到硬编码的HTML标记 我想通过Flask框架在HTML表格中显示员工ID、名字和姓氏 请参阅下面的Python代码: from flask import Flask, request, render_template import MySQLdb app = Flask(__name__) def db(): db = MySQ

如何用MySQL数据库中的值在HTML中填写{{}。该应用程序是用Flask编写的,Python版本为2.7.13。烧瓶0.12.2

当浏览到时,我只得到硬编码的HTML标记

我想通过Flask框架在HTML表格中显示员工ID、名字和姓氏

请参阅下面的Python代码:

from flask import Flask,  request, render_template
import MySQLdb

app = Flask(__name__)


def db():
    db = MySQLdb.connect(host="localhost",
                         user="someone",
                         passwd="something",
                         db="inventory" )

    cursor = db.cursor()

    cursor.execute("SELECT ID, First_Name, Last_Name from employees")

    rows = cursor.fetchall()

    db.close()

    return rows
    

rows = db()

for row in rows:
    #print(row)
    print str(row[0]) + " - " + row[1] + " " + row[2]

@app.route('/emp', methods=['GET'])
def emp():
        
    return render_template ('inventory.html')


if __name__ == '__main__':
    app.run(debug = True)
这是HTML模板

<!doctype html>
<html lang="en">
<head>
    <meta charset = "utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" />
    <link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.0/css/font-awesome.css" />
    <title>Inventory Database</title>
    <style type="text/css">
        body { background-color: white; }
    </style>
</head>

<body class="body">
    <div class="jumbotron text-center">
        <div class="page-header">
            <h3 class="text-primary">Inventory Management Database: All Employees </h3>
        </div>
        <table class="table table-striped table-bordered table-hover">
            <tr>
                <th>ID</th> 
                <th>First Name</th>
                <th>Last Name</th>              
            </tr>
            
            <tr>
                <th>1</th> 
                <th>Alex</th>
                <th>Smith</th>      
            </tr>
        
            {% for emp in employees %}
                <tr>
                    <td>{{ID}}</td>
                    <td>{{First_Name}}</td>
                    <td>{{Last_Name}}</td>
                </tr>
            {% endfor %}
        
            
                                    
                  
        </table>
        
    </div>  
   
</body>

</html>

库存数据库
正文{背景色:白色;}
库存管理数据库:所有员工
身份证件
名字
姓
1.
亚历克斯
史密斯
{员工%中的emp为%}
{{ID}
{{First_Name}}
{{Last_Name}}
{%endfor%}

如果要从模板访问变量
,则必须在调用
render\u template
时传递该变量。 只需在视图名称后添加参数

return render_template ('inventory.html', employees = rows)

检查文档:

尝试更改下面的这一行

发件人:
打印str(行[0])+“-”+行[1]+“+行[2]


打印
str(行[0]+“-”+行[1]+“+行[2])
我设法解决了这个问题,如下所示:

                    <th>Employee Name</th>
                    <th>Gender</th>
                    <th>Office</th>
                    <th>Comments</th>
                    <th>Edit</th>
                </tr>
            </thead>
              <tbody>
      {% for row in employees %}
            <tr>
              <td>
                        <span class="custom-checkbox">
                            <input type="checkbox" id="checkbox1" name="options[]" value="1">
                            <label for="checkbox1"></label>
                        </span>
              </td>
                <td>{{row[0]}}</td>
                <td>{{row[1]}}</td>
                <td>{{row[2]}}</td>
                <td>{{row[3]}}</td>
                <td>{{row[4]}}</td>
              <td>
员工姓名
性别
办公室
评论
编辑
{employees%%中的行的%s}
{{行[0]}
{{行[1]}
{{行[2]}
{{行[3]}
{{行[4]}

请阅读详细说明。谢谢tete0148。。。该网页现在看起来更好了,它有行,但它们只是空行,没有来自数据库的数据。您需要调试rows变量以检查其中的内容。完成后,只需修改inventory.html中的代码即可访问正确的objet属性