Inside my view.py, I have two functions, one that processes input from a form and outputs a filtered list, and another that is supposed to export this list to CSV.
Here is the return of my first function:
return render_to_response('templateX.html',
{
'queryset': queryset,
'filter_form': filter_form,
'validated': validated,
},
context_instance = RequestContext(request)
)
Here is the exporting function:
def export_to_csv(request):
# get the response object, this can be used as a stream.
response = HttpResponse(mimetype='text/csv')
# force download.
response['Content-Disposition'] = 'attachment;filename=export.csv'
# the csv writer
writer = csv.writer(response)
qs = request.session['queryset']
for cdr in qs:
writer.writerow([cdr['calldate'], cdr['src'], cdr['dst'], ])
return response
I'm not sure how to get the queryset from my first function, which contains a list of the items I want in my CSV and use it in my export_to_csv function. Or would the best way be combining these two functions and have the user click on a checkbox whether he/she wants to download a CSV file. Any help would be appreciated.