[英]How to display the result from SQLite db to html?
I am currently learning to build a small web-app.我目前正在学习构建一个小型网络应用程序。 I want to show the list of employees in the web page, and later the ability to add a new employee data to the database.我想在网页中显示员工列表,以及稍后将新员工数据添加到数据库的能力。 I am using SQlite db and Python Flask.我正在使用 SQlite db 和 Python Flask。 But I am stuck at this error message: UnboundLocalError: local variable 'rows' referenced before assignment
.但我被这个错误消息困住了: UnboundLocalError: local variable 'rows' referenced before assignment
。 So my question is, how do I fetch the content of my SQlite db and show them on my webpage?所以我的问题是,如何获取我的 SQlite 数据库的内容并将它们显示在我的网页上? I'd like to show it as a table as well.我也想将它显示为表格。
Here's my HTML code:这是我的 HTML 代码:
{% extends "layout.html" %}
{% block body %}
<h1> Employees </h1>
<ul>
{% for employee in employees %}
<li> {{ employee }}</li>
{% endfor %}
</ul>
<a href = "/add">Add a new employee</a>
{% endblock %}
And my python
code:还有我的python
代码:
@app.route("/")
def tasks():
rows = db.execute("SELECT * FROM employees")
return render_template("employee_list.html", rows= rows)
Anybody can help me?任何人都可以帮助我吗? Thank you.谢谢你。
use macro使用宏
{% macro render_comment(emp) %}
<li>{{ emp }}</li>
{% endmacro %}
<ul>
{% for emp in employees %}
{{ render_comment(emp) }}
{% endfor %}
</ul>
and change和改变
return render_template("employee_list.html", rows= rows)
to到
return render_template("employee_list.html", employees= rows)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.