Add CsvCarsDownload ClassView for exporting CSV files in Django
- Implemented a ClassView to handle CSV file downloads. - The view generates a CSV file with car data (brand, model, year). - Allows customization of CSV dialect and data structure. - Supports integration with other Class Mixins for additional functionalities.
This commit is contained in:
@@ -4,7 +4,6 @@ date: 2025-01-31T12:46:00+01:00
|
||||
feature_link: "https://www.midjourney.com/home/"
|
||||
feature_text: "by IA Midjourney"
|
||||
description: "A tool to start a life on the IndieWeb leaving the social media behind"
|
||||
isStarred: true
|
||||
tags:
|
||||
- blogging
|
||||
- indiweb
|
||||
|
||||
BIN
content/post/2025/csv-from-django/cover.png
Normal file
BIN
content/post/2025/csv-from-django/cover.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 1.3 MiB |
56
content/post/2025/csv-from-django/index.md
Normal file
56
content/post/2025/csv-from-django/index.md
Normal file
@@ -0,0 +1,56 @@
|
||||
---
|
||||
title: "Csv From Django"
|
||||
date: 2025-04-19T12:23:09+02:00
|
||||
feature_link: "https://www.midjourney.com/home/"
|
||||
feature_text: "by IA Midjourney"
|
||||
description: A little class view for return CSV
|
||||
isStarred: false
|
||||
tags:
|
||||
- csv
|
||||
- django
|
||||
categories:
|
||||
- dev
|
||||
- fingerfood
|
||||
images:
|
||||
keywords:
|
||||
series:
|
||||
- Django tricks
|
||||
|
||||
reply:
|
||||
repost:
|
||||
like:
|
||||
rsvp:
|
||||
bookmark:
|
||||
---
|
||||
|
||||
Some time you need to export a file into a specific format for some use like upload to the old system.
|
||||
In this case I need to have a CSV file which another software fill.
|
||||
|
||||
## The code
|
||||
|
||||
I wrote a ClassView for this case, this class. [^1]
|
||||
|
||||
[^1]: I don't like coding functional view so I only code ClassViews
|
||||
|
||||
``` python
|
||||
class CsvCarsDownload(View):
|
||||
def get(self, request, *args, **kwargs):
|
||||
response = HttpResponse(content_type="text/csv")
|
||||
response["Content-Disposition"] = 'attachment; filename="cars.csv"'
|
||||
my_dict = [{
|
||||
"brand": "Ford",
|
||||
"model": "Mustang",
|
||||
"year": 1964
|
||||
}]
|
||||
writer = csv.DictWriter(
|
||||
response, dialect="excel", fieldnames=my_dict[0].keys()
|
||||
)
|
||||
writer.writeheader()
|
||||
for element in my_dict:
|
||||
writer.writerow(element)
|
||||
return response
|
||||
```
|
||||
|
||||
You can change the type of the the CSV changing the __dialect__ with one of the type of the CSV dialect (excel, excel_tab, unix_dialect) and changing the __my_dict__ with any type of list of dict.
|
||||
|
||||
With this code you can use any Class Mixin from others module for add other functions like permission supports or loggeer configurations.
|
||||
Reference in New Issue
Block a user