我对此非常陌生,我遇到了一个TypeError。我试着用烧瓶模拟一个基本的网页。我已经设法将HTML请求从HTML文本框发送到我的程序。我试图在另一页的表格中显示响应。
这是我的输入页面:
<!DOCTYPE html>
<html>
<body>
<form action = "/Attendance" method = "POST">
<p>Enter Employee Code:</p>
<p><input type = "text" name = "employee_code" /></p>
<p><input type = "submit" value = "submit" /></p>
</form>
</body>
</html>
输入进入这个程序:
import pyodbc
from flask import Flask, request, render_template
app = Flask(__name__)
@app.route('/')
def attendance_fetch():
return render_template('Attendance_fetch.html')
@app.route('/Attendance', methods=['GET', 'POST'])
def database_file():
if request.method == 'POST':
req = request.form['employee_code']
conn = pyodbc.connect(
r'Driver={Microsoft Access Driver (*.mdb,
*.accdb)};DBQ=E:\xampp\htdocs\S10\SmartOffice.mdb;')
cursor = conn.cursor()
cursor.execute('select * from Employees where EmployeeCode =?', req)
for i in cursor.fetchall():
employee_id = str(i[0])
cursor.execute('select * from AttendanceLogs where EmployeeId=?', employee_id)
columns = [column[0] for column in cursor.description]
results = [columns] + [row for row in cursor.fetchall()]
for result in results:
return result
return render_template('Database_file.html', data=result)
if __name__ == '__main__':
app.run(debug=True)
我希望输出在这里显示为一个表:
<!DOCTYPE html>
<html>
<head>
<title>Attendance Table</title>
</head>
<body>
<table border = 1>
{% for key, value in data() %}
<tr>
<th> {{ key }} </th>
<td> {{ value }} </td>
</tr>
{% endfor %}
</table>
</body>
</html>
但我发现了错误:
“{rv.class.name}.".format(rv=rv)
TypeError:视图函数没有返回有效的响应。返回类型必须是字符串、dict、tuple、响应实例或可调用的WSGI,但它是一个列表。
发布于 2019-12-16 04:56:53
根据我对你问题的理解。您将在第一个Attendance_fetch.html
页面中获得一些ID,然后从数据库中获取详细信息,并希望在Database_file.html
页面中以表格格式显示该信息。
您的/Attendance
逻辑可能如下所示,您要在两个不同的列表中发送表头和值。
@app.route('/Attendance', methods=['GET', 'POST'])
def database_file():
if request.method == 'POST':
# DB Logic
headers = ["Name", "Emp code"]
values = ["Vikas Saini", "100"]
return render_template('Database_file.html', headers=headers, values=values)
然后在Database_file.html
中,您可以使用下面的代码来显示您的表。
<!DOCTYPE html>
<html>
<head>
<title>Attendance Table</title>
</head>
<body>
<table border = 1>
<tr>
{% for header in headers %}
<th> {{ header }} </th>
{% endfor %}
</tr>
<tr>
{% for value in values %}
<td> {{ value }} </td>
{% endfor %}
</tr>
</table>
</body>
</html>
发布于 2019-12-16 04:47:44
您返回错误的响应,应该是对象响应。只要移除线:
第一
columns = [column[0] for column in cursor.description]
results = [columns] + [row for row in cursor.fetchall()]
for result in results:
return result <-- !!!THIS LINE!!!
return render_template('Database_file.html', data=result)
第二,这是因为您试图用data()
调用list对象,只需删除数据中的括号。相反,data()
编写数据
{% for key, value in data %}
https://stackoverflow.com/questions/59356619
复制相似问题