首页 > 解决方案 > 如何在烧瓶中显示数据库列表中的值?

问题描述

我有一个问题,我想将数据库中的值显示到列表中,但这不起作用我尝试了几种方法,但仍然没有任何效果。

路线

@app.route('/t',methods=['GET', 'POST'])
def Consulta():
    g.con = pyodbc.connect('DRIVER={SQL Server}; SERVER=AUS_COMPUTO02\SQLEXPRESS; DATABASE=WEBSERVICE; Trusted_Connection = yes;')

    cur= g.con.execute("SELECT *FROM PROVEEDOR WHERE DECRIPCION  = '{}'".format('CHEF'))
    posts = [dict(ID_PROVEEDOR=row[0], DECRIPCION=row[1]) for row in cur.fetchall()] 
    g.con.close()
    print(posts)
    return render_template('consulta.html', posts=posts)

HTML

    <from name="des" action="{url_for('t')}" method='POST'>

        <select  name="des" >
       <option value="{{ID_PROVEEDOR}}">{{ DECRIPCION }} </option>
       <option value="2"></option>

        </select>

        <input type="submit">

         {% for p in post %}

        <strong>ID_PROVEEDOR:</strong> {{ p.ID_PROVEEDOR }}
        <strong>DECRIPCION:</strong> {{ p.DECRIPCION }} 
             {% endfor %} 
        </form>

标签: pythonhtmlflaskjinja2

解决方案


posts您在调用时使用帖子命名变量,但在模板中render_template引用它:post

{% for p in post %}

代替

{% for p in posts %}

请注意,变量会使用相应关键字参数的名称传递给 render_template:

render_template('consulta.html', posts=posts, foo="bar")  # posts and foo can be used in the template

DECRIPCION所以,你也需要ID_PROVEEDOR通过


推荐阅读