如何在Django的模板中显示我的视图输出变量?

时间:2018-02-13 11:05:09

标签: python django django-templates django-views

我有我的Django views.py函数:

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

上述函数过滤了dynamodb表中的UserId和Accountnumber项。我需要显示" UserId"和" AccountNum"在表格行中的html模板中。

这是我的html代码段:

<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">User ID</th>
                    <th class="th-lg">Account Number</th>
                </tr>
      </thead>

      <tbody>
              {% for r in rows %}
                <tr>
                    <th scope="row"></th>
                  <td>{{r.AccountNum}}</td>
                  <td>{{r.UserId}}</td>
               </tr>
               {% endfor %}
            </tbody>

        </table>

</div>

我在html代码中包含了阻止内容和endblock标记。我在这做错了什么?我是Django的初学者。提前致谢

1 个答案:

答案 0 :(得分:0)

您需要将输出呈现为html文件。

<强>实施例

from django.shortcuts import render_to_response

def dsctbl2(request):
    dynamodb=boto3.client('dynamodb', region_name='us-west-2')
    response = dynamodb.scan(
    TableName='User-Account')
    filtered = response['Items']
    length = len(filtered)
    a = []
    for k in range(length):
      accnum = filtered[k]['AccountNum']['S']
      uid = filtered[k]['UserId']['S']
      f = {}
      f = dict(AccountNum=accnum,UserId=uid)
      a.append(f)
    return render_to_response('your.html', {"rows":a})