Created
November 13, 2015 19:21
-
-
Save chrisforrette/415d04b1b6edbce9d813 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import csv | |
from django.http import HttpResponse | |
def serve_report(filename, fieldnames, data): | |
""" | |
Generate a CSV with the passed in `fieldnames` and `data` | |
and return a response with it as an attachment as `filename` + .csv | |
""" | |
response = HttpResponse(content_type='text/csv') | |
response['Content-Disposition'] = 'attachment; filename="%s.csv"' % filename | |
writer = csv.DictWriter(response, fieldnames=fieldnames) | |
writer.writeheader() | |
if len(data): | |
writer.writerows(data) | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment