如何显示flask中数据库列表中的值?

2024-06-28 20:24:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个问题,我想从我的数据库显示值列表,但这不起作用,我尝试了几种方法,没有什么仍然不起作用。你知道吗

路线

@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>

Tags: nameinidforpostconselectrow
1条回答
网友
1楼 · 发布于 2024-06-28 20:24:44

在调用render_template时,您将带有posts的变量命名为posts,但在模板中将其称为post

{% for p in post %}

而不是

{% for p in posts %}

请注意,变量传递给render\u template,其中包含相应关键字参数的名称:

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

所以,你也需要传递DECRIPCIONID_PROVEEDOR

相关问题 更多 >