Python Django如何循环对象并在模板中显示变量

Python Django如何循环对象并在模板中显示变量,python,django,amazon-web-services,amazon-s3,Python,Django,Amazon Web Services,Amazon S3,我正在使用boto3在AWS中显示有关s3存储桶的各种数据。 我在views.py中有以下代码来显示s3页面: class s3(TemplateView): template_name = 'project/s3.html' def get_context_data(self, **kwargs): context = super(s3, self).get_context_data(**kwargs) aws = boto3.reso

我正在使用boto3在AWS中显示有关s3存储桶的各种数据。 我在views.py中有以下代码来显示s3页面:

class s3(TemplateView):
     template_name = 'project/s3.html'

     def get_context_data(self, **kwargs):
         context = super(s3, self).get_context_data(**kwargs)
         aws = boto3.resource('s3')
         buckets = aws.buckets.all()
         for bucket in buckets:
             totalSize = 0
             bucketName = bucket.name
             createdAt = bucket.creation_date
             fileBuckets = boto3.resource('s3').Bucket(bucketName)
             for file in fileBuckets.objects.all():
                 totalSize += file.size

        context['buckets'] = buckets
        context['bucket'] = buckets
        context['createdAt'] = createdAt
        context['bucketName'] = bucketName
        context['totalSize'] = totalSize
        return context
我试图在这样的模板中显示这些变量:

<div class="s3Items">
  {% for bucket  in buckets %}
  <div class="s3Name">
    <div id="left">
      <h4 id='s3ItemName'>{{ bucketName }}</h4>
    </div>
    <div id="right">
      <ul id='s3ItemDesc'>
        <li>{{ createdAt }}</li>
        <li>{{ totalSize }}/4GB</li>
        <li>
          <button type="button" name="button" class='button delete'>Delete</button>
        </li>
      </ul>
    </div>
</div>
{% endfor %}

{bucket%中的bucket为%1}
{{bucketName}}
  • {{createdAt}}
  • {{totalSize}}/4GB
  • 删除
{%endfor%}
但很明显,这是行不通的。如何在模板中遍历这些桶? 我还尝试了以下方法,但效果并不完全,因为我无法获得每个存储桶中所有文件的总大小:

<div class="s3Items">
  {% for bucket  in buckets %}
  <div class="s3Name">
    <div id="left">
      <h4 id='s3ItemName'>{{ bucket.name }}</h4>
    </div>
    <div id="right">
      <ul id='s3ItemDesc'>
        <li>{{ bucket.creation_date}}</li>
        <li>{{ ??? }}/4GB</li>
        <li>
          <button type="button" name="button" class='button delete'>Delete</button>
        </li>
      </ul>
    </div>
</div>
{% endfor %}

{bucket%中的bucket为%1}
{{bucket.name}
  • {{bucket.creation_date}
  • {{???}}/4GB
  • 删除
{%endfor%}
我可以在模板内创建一个新循环吗?或者我应该在python文件中创建它并在模板中调用它?我该怎么做?
谢谢

您可以创建字典列表,然后可以在模板中迭代列表

class s3(TemplateView):
    template_name = 'project/s3.html'

    def get_context_data(self, **kwargs):
        context = super(s3, self).get_context_data(**kwargs)
        data = [] 
        aws = boto3.resource('s3')
        buckets = aws.buckets.all()
        for bucket in buckets:
            bucket_data = {}
            totalSize = 0
            fileBuckets = boto3.resource('s3').Bucket(bucketName)
            for file in fileBuckets.objects.all():
                totalSize += file.size
            bucket_data['bucketName'] = bucket.name
            bucket_data['createdAt'] = bucket.createdAt
            bucket_data['totalsize'] = totalSize
            data.append(bucket_data)
        context['buckets'] = data
        return context
现在,在模板中,您可以迭代变量“bucket”

<div class="s3Items">
  {% for bucket in buckets %}
  <div class="s3Name">
   <div id="left">
     <h4 id='s3ItemName'>{{ bucket.bucketName }}</h4>
   </div>
  <div id="right">
   <ul id='s3ItemDesc'>
     <li>{{ bucket.createdAt }}</li>
     <li>{{ bucket.totalsize }}/4GB</li>
     <li>
      <button type="button" name="button" class='button 
       delete'>Delete</button>
     </li>
   </ul>
  </div>
 </div>
{% endfor %}

{bucket%中的bucket为%1}
{{bucket.bucketName}
  • {{bucket.createdAt}}
  • {{bucket.totalsize}}/4GB
  • 删除
{%endfor%}
谢谢你,安妮塔,工作很有魅力!我没有想到要编一本字典。