title: How to download uploaded directory as a zip file from django admin?
body: <p>I have uploaded directory using django and JavaScript. Now I am trying to download this directory from django admin. For this purpose I have followed this link <a href="https://stackoverflow.com/questions/8080257/how-to-show-download-link-for-attached-file-in-filefield-in-django-admin/41520310#41520310">click here</a>. When I have uploaded directory in django, the admin created the following table. File name indicates the uploaded folder name.</p>
  <p><a href="https://i.stack.imgur.com/KeWGZ.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/KeWGZ.png" alt="enter image description here" /></a></p>
  <p>If I click on the file name, it shows me following error. I want to download this directory as a zip file from django admin. Can anyone please help me with this?</p>
  <p><a href="https://i.stack.imgur.com/QS9WI.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/QS9WI.png" alt="enter image description here" /></a></p>
  <p>Here is my <strong>model.py</strong>:</p>
  <pre><code>class Prearchive(models.Model):
      file = models.FileField(upload_to='media/', null=True) 
      file_name = models.CharField(max_length=500,primary_key=True)    
      file_id = models.ForeignKey(CreateScan, on_delete=models.CASCADE,default=1) 
  
      def _str_(self):
          return self.file_name 
  </code></pre>
  <p><strong>admin.py:</strong></p>
  <pre><code>from django.contrib import admin
  from .models import CreateScan, Prearchive
  
  # Register your models here.
  
  class PrearchiveAdmin(admin.ModelAdmin):
      model=Prearchive
      list_display = ('file_name','file_id','file_link')
      def file_link(self, obj):
          if obj.file:
              return &quot;&lt;a href='%s' download&gt;Download&lt;/a&gt;&quot; % (obj.file.url,)
          else:
              return &quot;No attachment&quot;
      file_link.allow_tags = True
      file_link.short_description = 'File Download'
  
  admin.site.register(Prearchive , PrearchiveAdmin)
  </code></pre>
  <p><strong>view.py:</strong></p>
  <pre><code>@login_required
  def downloadScanView(request,url):
      print('url',url)
      response = HttpResponse(content_type='application/zip')
      file_path = os.path.join(settings.MEDIA_ROOT, url)
      zip_download = ZipFile(response, 'w',ZIP_DEFLATED)
      if os.path.exists(file_path):
          for root, dir, files in os.walk(file_path):
              print('r = ',root)
              for file in files:
                  zip_download.write(os.path.join(root,file))
          response['Content-Disposition'] = 'attachment; filename={}'.format(os.path.basename(url)+'.zip')
          return response
  </code></pre>