Django how to loop through objects and display variables in a template

Viewed 2540

I'm working with boto3 to display various data about my s3 buckets in AWS. I have the following code in views.py to display s3 page:

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

I'm trying to display these variables in a template like this:

<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 %}

But obviously this doesn't work. How can I iterate through those buckets in template? I also tried the below and it worked but not completely as I cannot get the total sizes of all files in each bucket:

<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 %}

Can I create a new loop inside the template? Or should I create it in python file and call it in template? How can I do this? Thanks

1 Answers

You can create a list of dictionary and then can iterate over list in the template.

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

Now in template you can iterate over variable 'buckets'.

<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 %}
Related