Here is my problem. To summarize, my application transform one or many images. And I want that the user can download the images transformed with a button "Save new images" for example. When the user clicks the button, the Download window for example pops up and he can download all the images where he wants on his computer. I don't know if it's possible, I wait your answers.
I already did the upload images part, it works.
views.py
def mon_data(request, dataset_id):
my_data = get_object_or_404(Dataset, pk=dataset_id)
images_src = Image_Source.objects.filter(dataset = my_data)
images_seg = []
for src in images_src:
images_seg.append(Image_Segmentee.objects.get(origine = src, model = src.model_initial))
model = images_seg[0].model
context = {
"material": model.materiau_type, "model": model,
"images_seg": images_seg, "my_data": my_data, "nb_images":len(images_seg)
}
return render(request, "interface/my_data.html", context)
models.py
TYPES=[
('Metal', 'Metal'),
('Composite', 'Composite'),
]
DECISION=[
('En attente', 'En attente'),
('Valide', 'Valide'),
('Rejete', 'Rejete'),
]
class Modele(models.Model):
name = models.CharField(max_length=100)
materiau_type = models.CharField(max_length=20, choices=TYPES, null=False, blank=False)
class Dataset(models.Model):
name = models.CharField(max_length=200, null=False, blank=False)
date = models.DateField()
class Image_Source(models.Model):
dataset = models.ForeignKey(Dataset, on_delete=models.CASCADE, null=True)
materiau_type = models.CharField(max_length=20, choices=TYPES, null=False, blank=False)
model_initial = models.ForeignKey(Modele, on_delete=models.SET_NULL, null=True)
illustration = models.ImageField(null=False, blank=False, upload_to="img/")
class Image_Segmentee(models.Model):
dataset = models.ForeignKey(Dataset, on_delete=models.CASCADE, null=True)
origine = models.ForeignKey(Image_Source, on_delete=models.CASCADE, null=True)
operator_decision = models.CharField(max_length=20, choices=DECISION, default='En attente')
illustration = models.ImageField(null=False, blank=False, upload_to="img_modif/")
model = models.ForeignKey(Modele, on_delete=models.SET_NULL, null=True)
interface/my_data.html
<div class="row">
<div class="col s6">
<p style="font-size: 200%; font-weight: bold;">Original image</p>
{% for seg in images_seg %}
<a href="{% url 'mon_image' seg.origine.id %}">
<p style="color: black;">{{seg.origine}}</p>
<img class="img-data" src="{{ seg.origine.illustration.url }}" alt="image_src_echec">
</a>
{% endfor %}
</div>
<div class="col s6">
<p style="font-size: 200%; font-weight: bold;">Segmented image</p>
{% for seg in images_seg %}
<a href="{% url 'mon_image' seg.origine.id %}">
<p style="color: black;">{{seg}}</p>
<img class="img-data test1" src="{{ seg.illustration.url }}" alt="image_seg_echec">
</a>
{% endfor %}
</div>
</div>
-- Edit --
I created a new view download and I changed my urls.py file. Don't forget the imports
views.py
from wsgiref.util import FileWrapper
from zipfile import ZipFile
def download_user(request, dataset_id):
my_data = get_object_or_404(Dataset, pk=dataset_id)
images_seg = Image_Segmentee.objects.filter(dataset = my_data)
with ZipFile('export.zip', 'w') as export_zip:
for my_seg in images_seg:
img_url = "." + my_seg.illustration.url
print(img_url.split("/")[-1])
export_zip.write(img_url, img_url.split('/')[-1])
wrapper = FileWrapper(open('export.zip', 'rb'))
content_type = 'application/zip'
content_disposition = 'attachment; filename=export.zip'
response = HttpResponse(wrapper, content_type=content_type)
response['Content-Disposition'] = content_disposition
return response