django在cmd中打印所有记录,但在模板上显示最后一个记录

时间:2019-10-11 06:25:50

标签: django for-loop django-templates fetchall

我有一个功能,该功能从我正在使用 fetchall()的sql server数据库中检索数据,然后进行循环 检索到的数据全部打印在 cmd控制台上,但是当我尝试在模板上显示这些结果时,它仅显示最后一条记录

views.py

def搜索(请求):

  query = cursor.execute('Select id, fullname from person')
  result = query.fetchall()
  for row in result:
    print("ID==>",id)
    IPD=row[0]
  return render(request,"test.html",{"IPD":IPD})

test.html

  {{ IPD }}

1 个答案:

答案 0 :(得分:0)

 query = cursor.execute('Select id, fullname from person')
 result = query.fetchall()
 IPD=[]
 for row in result:
     print("ID==>",id)
     IPD.append(row)
 return render(request,"test.html",{"IPD":IPD})