Flask를 사용하여 Postgresql 데이터베이스에서 데이터를 가져오고 HTML 테이블을 표시하는 방법

Kohila
  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가 외부 들여 쓰기 수준과 일치하지 않습니다.

이 플라스크 파일을 실행할 때 많은 오류가 발생합니다. 데이터베이스를 제대로 연결하고 복잡한 SQL 쿼리로 데이터를 가져 와서 HTML 파일에 표시하는 방법을 알고 싶습니다. 진행 방법을 알려주세요.

Bernardo Stearns 여행

이것은 코드의 논리와 관련이 없습니다. 코드의 들여 쓰기는 다음과 같아야합니다. 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] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

Related 관련 기사

뜨겁다태그

보관