简体   繁体   中英

How do I display the views output on my template in tabular rows?

I am using Django as my web framework. I have written a views function like this.. to fetch a list of all items under the keys UserId and AccountNum from an AWS DynamoDb table :

def dsctbl2(request):
  dynamodb=boto3.client('dynamodb', region_name='us-west-2')
  response = dynamodb.scan(
    TableName='User-Account')
  filtered = response['Items']
  length = len(filtered)
  for k in range(length):
    accnum = filtered[k]['AccountNum']['S']
    uid = filtered[k]['UserId']['S']
    f = dict(AccountNum=accnum,userID=uid)
    rows = []
    for k,v in f.items():
      rows.append(k)
      rows.append(v)

  return render(request,'useradd.html',{"rows": rows})

The rows variable yields a set of lists like this :

['AccountNum', '873627867348', 'userID', 'abc']
['AccountNum', '873627867348', 'userID', 'def']
['AccountNum', '038683828978', 'userID', 'ghi']
['AccountNum', '581889263266', 'userID', 'jkl']
['AccountNum', '581889263266', 'userID', 'mno']
['AccountNum', '581889263266', 'userID', 'pqr']
['AccountNum', '581889263266', 'userID', 'stu']
['AccountNum', '977201296795', 'userID', 'vwx']
['AccountNum', '232686542773', 'userID', 'yza']

I need to display these values in my table in my HTML template. The HTML table snippet is below :

 <div class="mytable">
  <table style="width:96%" class="table table-responsive">
   <thead id="head" class="mdb-color lighten-4">
                <tr>
                    <th></th>
                    <th class="th-lg">Account #</th>
                    <th class="th-lg">User Id</th>
                </tr>
   </thead>
   <tbody>

                <tr>
                    <th scope="row"></th>
                  <td>{{rows.AccountNum}}</td>
                  <td>{{rows.UserId}}</td>
               </tr>

  </tbody>
</table>
</div>

But when I hit that html page in my browser , I can see only the table headings 'UserId' and 'AccountNum'. Why aren't the UserId and AccounNum values getting printed ? I have added blockcontent and endblock tags in my html template. I am a novice in Django and don't understand the concepts very well. Pardon me if the question sounds silly. Can anyone please help me ? I am stuck with this problem. Thank you in advance.

Few things to be considered please don't create list inside for loop because that will not work ( not save all in list ).

rows = []
for each in range(length):
    accnum = filtered[k]['AccountNum']['S']
    uid = filtered[k]['UserId']['S']
    f = dict(AccountNum=accnum,userID=uid)
    rows.append(f)

return render(request, 'useradd.html', {'rows': rows})

And in html you can use Django template language for loop to print all the variables.

<table>
  <thead>
    <tr>
      <th>#</th>
      <th>Account</th>
      <th>User</th>
    </tr>
   </thead>
   <tbody>
     {% for row in rows %}
       <tr>      
        <td>{{ row.AccountNum }}</td>
        <td>{{ row.UserId }}</td>
       </tr>
     {% endfor %}
   </tbody>
</table>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM