How to use the passed value (list/array) in Django View? I have tried something like this:
def to_csv(request):
g_data = request.GET.getlist('json_data')
g_header = request.GET.get('header')
g_info = request.GET.get('info')
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="somefilename.csv"'
list_header = [[g_info, '', '', '', '', '', '', ''],
['Municipality', 'Barangay', 'Total Number of Structures', 'Total Not Affected', 'Total Affected',
'Low', 'Medium', 'High', 'Total Affected %']]
disclaimer = [
'Disclaimer: The information....',
'', '', '', '', '', '']
for val in g_data:
perc = (val[4] / float(val[2])) * 100
st_perc = str(round(perc, 2))
val.append(st_perc)
list_header.append(val)
list_header.append(disclaimer)
writer = csv.writer(response)
writer.writerows(list_header)
return response
and in my JavaScript code using AJAX:
function to_csv(json_data, header, info){
$.ajax({
url: "/to_csv/",
headers: {
Accept : "text/csv; charset=utf-8",
"Content-Type": "text/csv; charset=utf-8"
},
type: "GET",
data: {
'json_data': json_data,
'header': header,
'info':info
},
The problem is, it does not get the passed data (g_data)