How to Create Zip File and Download in Django?

Published On: 02/08/2022 | Category: Django Python

Hi Dev,

Now, let's see example of django create zip file. you will learn how to create zip file and download in django. you can understand a concept of django create zip archive file and download it in response. step by step explain create a zip file from a folder and download in django application. Let's see bellow example create zip file from folder and download in django application.

In this example, we will use to zipfile library in python so we can help fully to download zip file.let's see bellow example create zip file from folder and download in django application

You can use these examples with django3 (django 3) version.

let's see below simple example with output:

Step 1 : Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject example
Step 2 : Create a App
python3 startapp core
Step 3 : Update

Here, do not forget to register the new app in the file. Under installed apps, just add ‘core’ to the list:

Step 4 : Database Setup

Next step, we will modify the file and update the database settings to configure the mydb database:
    'default': {  
        'ENGINE': 'django.db.backends.mysql',  
        'NAME': 'example',  
Step 5: Create a Model

In this step we will require the database model for storing contacts.Open the core/ file and add the following code:

from django.db import models

class Blog(models.Model):
    name = models.CharField(max_length=100)
    description = models.TextField()
    created_at = models.DateTimeField(auto_now_add=True)

    def __str__(self):

    class Meta:
        ordering = ['-created_at']

After creating these model, you need to create migrations using the following command:

Step 6 : Create a Migrations and Migrate
python makemigrations
python migrate
Step 7 : Django’s admin interface

To be able to work with the new model from the admin interface we need to register it. Add this code to the core/ module:

from django.contrib import admin

from .models import Blog

class BlogAdmin(admin.ModelAdmin):
    pass, BlogAdmin)
Step 8 : Creating the Views

In this step, we need to create the views for performing ajax image upload to the database.Open the core/ file and add.

import zipfile
from django.http import HttpResponse
from .models import Blog

Welcome to our Website!!

def downloadZip(request):
    """Download archive zip file of code snippets"""
    response = HttpResponse(content_type='application/zip')
    zf = zipfile.ZipFile(response, 'w')

    # create the zipfile in memory using writestr
    # add a readme

    # retrieve snippets from ORM and them to zipfile
    blogs = Blog.objects.all()
    for blog in blogs:
        zf.writestr(, blog.description)

    # return as zipfile
    response['Content-Disposition'] = f'attachment; filename={ZIPFILE_NAME}'
    return response
Step 9 : Creating URLs

In this section, we’ll create the urls to access our views.Go to the core/ file and update it as follows:

from django.urls import path
from . import views

urlpatterns = [
    path('download-zip/', views.downloadZip, name='download')

Next, we will require the modify the your root preoject folder lets update the file.

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('core.urls'),
Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python runserver

Next, go to the address with a web browser.


I Hope It will help you....