【问题标题】:How do I resolve this Python Flask TypeError? Where did I go wrong?如何解决这个 Python Flask TypeError?我哪里做错了?
【发布时间】:2020-04-08 21:59:25
【问题描述】:

我对此很陌生,但遇到了 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:视图函数未返回有效响应。返回类型必须是字符串、字典、元组、响应实例或 WSGI 可调用,但它是一个列表。

【问题讨论】:

    标签: python html flask


    【解决方案1】:

    你返回错误的响应,应该是对象响应。 只需删除行:

    第一

    columns = [column[0] for column in cursor.description]
    results = [columns] + [row for row in cursor.fetchall()]
    for result in results:
        return result   <-- !!!!!REMOVE THIS LINE!!!!!
        return render_template('Database_file.html', data=result)
    

    第二 这是因为您尝试使用 data() 调用列表对象只需从数据中删除括号。而是 data()data

    {% for key, value in data %} 
    

    【讨论】:

    • 感谢您的回复。我试过了,但现在它说 {% for key, value in data() %} TypeError: 'list' object is not callable
    • 这是因为您试图用data() 调用列表对象只需从data 中删除括号。取而代之的是data()data ``` {% for key, value in data %} ```
    【解决方案2】:

    根据我对您的问题的理解。您在第一个 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-04
      • 1970-01-01
      • 2021-12-15
      • 2015-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多