Python 如何使用Flask从Postgresql数据库中获取数据并显示HTML表

Python 如何使用Flask从Postgresql数据库中获取数据并显示HTML表,python,postgresql,flask,Python,Postgresql,Flask,test.html import psycopg2 from flask import Flask, render_template app = Flask(__name__) con = psycopg2.connect(database="Angels_Attic", user="postgres", password="", host="127.0.0.1", port="5432") cursor = con.cursor() @app.ro

test.html

  import psycopg2
    from flask import Flask, render_template
    app = Flask(__name__)
    con = psycopg2.connect(database="Angels_Attic", user="postgres", password="", host="127.0.0.1", port="5432")
    cursor = con.cursor()

    @app.route("/", methods=['post', 'get'])
    def test():  
        cursor.execute("select * from cart")
        result = cursor.fetchall()
        return render_template("test.html", data=result)

{数据%中的行为%1}
{{行[0]}

{{行[1]}

{%endfor%}
我得到的错误是: 缩进错误:未缩进与任何外部缩进级别不匹配


当我运行这个flask文件时,我得到了一堆错误。我只想知道如何正确连接数据库,使用复杂的SQL查询从数据库中获取数据,并将其显示在HTML文件中。请告诉我如何继续。

这与代码的逻辑无关,只是代码的缩进应该是这样的: 删除导入下面所有行中的空格

<html>
<body>
    <div>
        {% for row in data %}
        <p>{{ row[0] }}</p>
        <p>{{ row[1] }}</p>
        {% endfor %}
    </div>

    </body>
    </html>

这与代码的逻辑无关,只是代码的缩进应该是这样的: 删除导入下面所有行中的空格

<html>
<body>
    <div>
        {% for row in data %}
        <p>{{ row[0] }}</p>
        <p>{{ row[1] }}</p>
        {% endfor %}
    </div>

    </body>
    </html>