I have a list of S3 buckets on AWS. Each bucket's name is displayed and I'm trying to implement a delete button using django. The problem I'm having is that I'm not sure yet how to get and pass bucket name variable to the delete function I have. Here's what I've done so far:
DeleteBucketView.py
class deleteBucket(TemplateView):
template_name = "project/delete_bucket.html"
def deleteBucket(self, name):
s3 = boto3.resource('s3')
bucket = s3.Bucket(name)
bucket.delete()
S3.html
<div class="s3Items">
{% for bucket in buckets %}
<div class="s3Name">
<div id="left">
<a href="#"><h4 id='s3ItemName'>{{ bucket.bucketName }}</h4></a>
</div>
<div id="right">
<ul id='s3ItemDesc'>
<li>{{ bucket.createdAt }}</li>
<li>{{ bucket.totalSize }}/4GB</li>
<li>
<form method="post" action="{% url 'project:deleteBucket' %}">
{% csrf_token %}
<button type="submit" name="button" class='button delete'>Delete</button>
</form>
</li>
</ul>
</div>
</div>
{% endfor %}
deletebucket.html is empty as I'm not sure what I could enter there. I need to call function deleteBucket
when I click delete button, however I would need to pass bucket's name to the function as well. I think I also need to define post in deleteBucket view but I'm not sure how to go from there. How do I pass the bucket's name into the function and make this work? Do I need to add anything in deleteBucket.html?
Thanks
I don't know much about your url definition but I assume like this...
url(r'^delete-bucket/(?P<name>\w+)$', 'views.deleteBucket.as_view()', name='delete_bucket'),
And modify your view like this...
class deleteBucket(TemplateView):
template_name = "project/delete_bucket.html"
def delete_operation(self, name):
s3 = boto3.resource('s3')
bucket = s3.Bucket(name)
bucket.delete()
def post(self, request, name):
self.delete_operation(name)
...
And in the html you have to tell the name
<form method="post" action="{% url 'project.delete_bucket' name=bucket.bucketNmae %}">
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.