Skip to content

Instantly share code, notes, and snippets.

@bencleary
Last active January 27, 2018 14:10
Show Gist options
  • Select an option

  • Save bencleary/b6cf31b26a70c0b45ca367ba9782bf2d to your computer and use it in GitHub Desktop.

Select an option

Save bencleary/b6cf31b26a70c0b45ca367ba9782bf2d to your computer and use it in GitHub Desktop.
class Csv(View):
def get(self, request):
sales = Sales.objects.all()
headers = [(h.column) for h in Sales._meta.get_fields()]
csv_array = Generate(request).csv_file(headers=headers, data=sales)
if os.path.exists(csv_array[1]):
with open(csv_array[1], 'r') as csv:
response = HttpResponse(csv.read(), content_type="text/csv")
response["Content-Disposition"] = f'attachment; filename={csv_array[0]}'
return response
else:
return HttpResponse("File Does Not Exist")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment