22 – HOW TO EXPORT DATA TO CSV OR EXCEL IN DJANGO

Spread the love

1. In forms.py, add a form field in the search form

export_to_CSV = forms.BooleanField()

2. In views.py, import HttpResponse from django.http, and csv

from django.http import HttpResponse
import csv

3. Make an if condition that will export data to CSV

		if form['export_to_CSV'].value() == True:
			response = HttpResponse(content_type='text/csv')
			response['Content-Disposition'] = 'attachment; filename="List of stock.csv"'
			writer = csv.writer(response)
			writer.writerow(['CATEGORY', 'ITEM NAME', 'QUANTITY'])
			instance = queryset
			for stock in instance:
			 writer.writerow([stock.category, stock.item_name, stock.quantity])
			return response

Spread the love

About the author

arbadjie

Hi, I'm Abdourahman Badjie, an aspiring developer with obsession for anything coding and networking. This blog is dedicated to helping people learn to develop web applications using django framework.

View all posts

11 Comments

  • Hey Arbadjie. I have the same code as yours but once I included the category as a ForeignKey field in the Stock model, my Search functionality does not work anymore. I get this error:

    FieldError at /list_items/
    Related Field got invalid lookup: icontains

    C:\Users\Eugene Kwaka\Desktop\TUTORIALS\StockManagementSystem\stockmanagement\stockmgmt\views.py, line 25, in list_items
    item_name__icontains=form[‘item_name’].value()) …
    ▶ Local vars

  • i have an error in my code KeyError at /stocks/
    “Key ‘export’ not found in ‘StockSearchForm’. Choices are: category, export_to_cvs, nom.”
    Request Method: POST
    Request URL: http://127.0.0.1:8000/stocks/
    Django Version: 3.2.6
    Exception Type: KeyError
    Exception Value:
    “Key ‘export’ not found in ‘StockSearchForm’. Choices are: category, export_to_cvs, nom.”
    Exception Location: C:\Users\belibi\AppData\Local\Programs\Python\Python39\lib\site-packages\django\forms\forms.py, line 155, in __getitem__
    Python Executable: C:\Users\belibi\AppData\Local\Programs\Python\Python39\python.exe
    Python Version: 3.9.6
    Python Path:
    [‘C:\\Users\\belibi\\Desktop\\django-finances app\\main’,
    ‘C:\\Users\\belibi\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip’,
    ‘C:\\Users\\belibi\\AppData\\Local\\Programs\\Python\\Python39\\DLLs’,
    ‘C:\\Users\\belibi\\AppData\\Local\\Programs\\Python\\Python39\\lib’,
    ‘C:\\Users\\belibi\\AppData\\Local\\Programs\\Python\\Python39’,
    ‘C:\\Users\\belibi\\AppData\\Local\\Programs\\Python\\Python39\\lib\\site-packages’]
    Server time: Sun, 19 Sep 2021 20:27:45 +0000

  • please help me out

    FieldError at /list_item/
    Related Field got invalid lookup: icontains
    Request Method: POST
    Request URL: http://127.0.0.1:8000/list_item/
    Django Version: 2.2
    Exception Type: FieldError
    Exception Value:
    Related Field got invalid lookup: icontains
    Exception Location: C:\Project\STOCK\lib\site-packages\django\db\models\sql\query.py in build_lookup, line 1107
    Python Executable: C:\Project\STOCK\Scripts\python.exe
    Python Version: 3.9.6
    Python Path:
    [‘C:\\Project\\STOCK\\djangoStock’,
    ‘C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python39\\python39.zip’,
    ‘C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python39\\DLLs’,
    ‘C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python39\\lib’,
    ‘C:\\Users\\hp\\AppData\\Local\\Programs\\Python\\Python39’,
    ‘C:\\Project\\STOCK’,
    ‘C:\\Project\\STOCK\\lib\\site-packages’]

Leave a Reply to lucky Cancel reply

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