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)
test.html
<html>
<body>
<div>
{% for row in data %}
<p>{{ row[0] }}</p>
<p>{{ row[1] }}</p>
{% endfor %}
</div>
</body>
</html>
我得到的错误:IndentationError:unindent与任何外部缩进级别都不匹配
当我运行这个flask文件时,出现了很多错误。我只想知道如何正确连接数据库,并使用复杂的SQL查询从数据库中获取数据并将其显示在HTML文件中。请让我知道如何进行。
这与代码的逻辑无关,只是代码的缩进应该是这样的:从import下面的所有行中删除空格
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)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句