postgre 生成数据库html文档_从PostgreSQL到Django获取数据并以html格式显示

我在PostgreSQL数据库中有一个表,其中包含两列ID和Name.我使用django框架从数据库中获取数据,我想将数据显示到html页面.问题是检索到的数据没有列名.它看起来像这样,并且为了在html页面中使用它,它必须有一个每个元组的键.

[(2, 'abc'), (3, 'subhi')]

我试图获取列名称,但它们只是没有数据的表列.以下是我的代码:

models.py

import psycopg2

import pprint

def main():

conn_string = "host='localhost' dbname='music' user='postgres' password='subhi123'"

column_names = []

data_rows = []

with psycopg2.connect(conn_string) as connection:

with connection.cursor() as cursor:

cursor.execute("select id, name from music")

column_names = [desc[0] for desc in cursor.description]

for row in cursor:

data_rows.append(row)

records = cursor.fetchall()

# print out the records using pretty print

# note that the NAMES of the columns are not sh

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值