简体   繁体   中英

How to export zip in django admin?

I have images fields in my model. How to I can export to zip from my django admin. Here is my code

from django.db import models


class ModelWithImage(models.Model):
    image = models.ImageField(
        upload_to='images',
    )



Admin

from django.contrib import admin
from .models import ModelWithImage

admin.site.register(ModelWithImage)

Something like this is used to create zipfile for content in files (path) in filenames .

response = HttpResponse(content_type='application/zip')
zip_file = zipfile.ZipFile(response, 'w')
for filename in filenames:
    zip_file.write(filename)
response['Content-Disposition'] = 'attachment; filename={}'.format(zipfile_name)
return response  

This code is taken from here , you may have a look for better understanding. Although I think above code should solve your problem.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM