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
Bro Please upload changing Django Admin Template videos .
I haven’t done a video on that yet. I might look into that
really good content, can i ask something? why my CSV file saved not in a table , but just a paragraph
CSV is data in rows separated by comma (,). I believe that made it look live paragraph. Probably you opened it in a text editor
How to Import CSV & Excel Files. I am trying, but not able to do.
I will do a video about that soon inshaAllah
step3 show identation error how i solve this
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 a problem
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’]