0

i have a function that retrieve data from sql server database where i am using fetchall() then for loop the retrieved data are all printed on the cmd console but when i tried to display these result on the template it only display the last record.

views.py

def search(request):

  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 }}
Dev Dj
  • 169
  • 2
  • 14

1 Answers1

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})
Vaibhav Mishra
  • 227
  • 2
  • 11
  • Thank you for this code snippet, which might provide some limited, immediate help. A [proper explanation](https://meta.stackexchange.com/q/114762/349538) would greatly improve its long-term value by showing why this is a good solution to the problem and would make it more useful to future readers with other, similar questions. Please [edit] your answer to add some explanation, including the assumptions you’ve made. – CertainPerformance Oct 12 '19 at 08:55