How to download data as CSV and Excel file in Django

download data as csv and excel file in Django

Sometimes we are required to dump database data in CSV or Excel file. In this article we will see how to download the data in CSV or Excel file in Django.

Download data as Excel file in Django:

For downloading data in excel file we need to add xlwt  package in our environment. I assume you are using virtual environment to develop your django app. It is always recommended to user virtual environment.

Once virtual environment is activated, Run this command to add xlwt  package.

Inside your view , import xlwt  package.

Use below code in your view in  file to create and download excel file.


Download data as CSV file in Django:

Import csv  and smart_str  package in your view. Use below code to download the data in CSV format.


Let us know by commenting if you are facing any issue.

Host your Django App on PythonAnyWhere for Free.



(Visited 1,863 times, 1 visits today)

You must read this :

Leave a Reply

Your email address will not be published. Required fields are marked *