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>
解决方案
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
通过
推荐阅读
- java - 在服务中配置线程池大小
- c# - VS for Mac 扩展 - ActiveWindow 中的空编辑器
- ruby-on-rails - 空 zip 存档:找不到用于摘要的模板:项目/存储库/存档
- material-ui - material-ui 用 Link 包装 Tab 组件
- html - 在 CSS 中包装元素
- javascript - 如何在不启动新会话(Selenium)的情况下从 Node.js 调用 Python 函数?
- r - ggplot2 - 按颜色排序条形图
- haskell - 从 Prelude 隐藏实例声明
- java - Java 中的 GC 成本
- javascript - bal 不是函数 (DISCORD.js)