python - 使用 spycopg2 Python Flask 填充 HTML Select
问题描述
我是使用烧瓶的新手,我想提取该数据并使用 FLASK 以 html 形式将其显示为选择选项。这是我的选择
<span class="input-group-addon">Unity</span>
<select name="unity" class="selectpicker form-control">
</select>
我想用这些数据填充它
@app.route("/stadistics/unity")
def unity():
db = get_db()
cursor = db.cursor()
cursor.execute("select unity from mse.unity")
unity = cursor.fetchall()
print (unity)
我怎么能这样做?
解决方案
Python代码:
@app.route("/stadistics/unity")
def unity():
db = get_db()
cursor = db.cursor()
cursor.execute("select unity from mse.unity")
unity = cursor.fetchall()
return render_template('my_html_file.html', unity=unity)
HTML,带有 Jinja2 for 循环:
<select name="unity" class="selectpicker form-control">
{% for u in unity %}
<option value="{{ u }}">{{ u }}</option>
{% endfor %}
</select>
Flask 默认包含 Jinja2 模板引擎,您可以将您选择的变量传递给您的 HTML 模板,当您调用时render_template()
,请查看文档以获取更多详细信息:https ://flask.palletsprojects.com/en/1.1.x /quickstart/#rendering-templates
推荐阅读
- android - 如何在 Android Studio 中添加平板电脑分辨率支持
- swagger - 在 Nest.js 项目中添加 Swagger 基本路径字段
- mysql - 在mysql中选择多个表的公共值
- .net - 您可以从常规 .net 应用程序中使用 Roslyn 编译器吗?
- python - 使用 Socket 网络后端创建 QEMU 桥接器
- sql - 将多个 SQL Server 表合并为一个
- machine-learning - 如果我添加一个新特性,xgboost 参数会不会要改变?
- android - 会话结束时删除图像和 pdf
- mysql - mysql/mongodb集群适合安装在kubernetes上吗?
- reactjs - React 和 Typescript,样式化的组件和子组件