2

I'm using django and I'm trying to export the CSV_data list into csv file. Below is my csv.py:

#coding=utf-8
from django.http import HttpResponse
from django.template import loader, Context
from demo.views import CSV_data


def output(request, filename):
    response = HttpResponse(mimetype='text/csv')
    response['Content-Disposition'] = 'attachment; filename=%s.csv' % filename

    t = loader.get_template('csv.txt')
    c = Context({
        'data': CSV_data,
    })
    response.write(t.render(c))
    return response

CSV_data is a variable in views.py, I tried to print it in template, the value is ok.

 [(u'2012-06-01', [0, 0, 0]), ('2012-06-08', [0, 0, 0]), ('2012-06-15', [0, 0, 0]),    ('2012-06-22', [0, 0, 0]), ('2012-06-29', [0, 0, 0]), ('2012-07-06', [0, 0, 0]), ('2012-07-13', [0, 0, 0]), ('2012-07-20', [0, 0, 0]), ('2012-07-27', [0, 0, 0]), ('2012-08-03', [131, 164, 79.88]), ('2012-08-10', [110, 198, 55.56]), ('2012-08-17', [112, 197, 56.85]), ('2012-08-24', [147, 283, 51.94]), ('2012-08-31', [0, 306, 0.0]), ('2012-09-07', [418, 418, 100.0]), ('2012-09-14', [342, 342, 100.0]), ('2012-09-21', [732, 732, 100.0]), ('2012-09-28', [689, 689, 100.0]), ('2012-10-05', [775, 775, 100.0]), ('2012-10-12', [469, 469, 100.0]), ('2012-10-19', [477, 477, 100.0]), ('2012-10-26', [897, 897, 100.0]), ('2012-11-02', [216, 216, 100.0]), ('2012-11-09', [1046, 1046, 100.0]), ('2012-11-16', [840, 840, 100.0]), ('2012-11-23', [948, 948, 100.0])]

However, the generated csv is always empty.

I tried to add the CSV_data definition to the csv.py file, like this:

#coding=utf-8
from django.http import HttpResponse
from django.template import loader, Context

CSV_data = [(u'2012-06-01', [0, 0, 0]), ('2012-06-08', [0, 0, 0]), ('2012-06-15', [0, 0, 0]), ('2012-06-22', [0, 0, 0]), ('2012-06-29', [0, 0, 0]), ('2012-07-06', [0, 0, 0]), ('2012-07-13', [0, 0, 0]), ('2012-07-20', [0, 0, 0]), ('2012-07-27', [0, 0, 0]), ('2012-08-03', [131, 164, 79.88]), ('2012-08-10', [110, 198, 55.56]), ('2012-08-17', [112, 197, 56.85]), ('2012-08-24', [147, 283, 51.94]), ('2012-08-31', [0, 306, 0.0]), ('2012-09-07', [418, 418, 100.0]), ('2012-09-14', [342, 342, 100.0]), ('2012-09-21', [732, 732, 100.0]), ('2012-09-28', [689, 689, 100.0]), ('2012-10-05', [775, 775, 100.0]), ('2012-10-12', [469, 469, 100.0]), ('2012-10-19', [477, 477, 100.0]), ('2012-10-26', [897, 897, 100.0]), ('2012-11-02', [216, 216, 100.0]), ('2012-11-09', [1046, 1046, 100.0]), ('2012-11-16', [840, 840, 100.0]), ('2012-11-23', [948, 948, 100.0])]

def output(request, filename):
    response = HttpResponse(mimetype='text/csv')
    response['Content-Disposition'] = 'attachment; filename=%s.csv' % filename

    t = loader.get_template('csv.txt')
    c = Context({
        'data': CSV_data,
    })
    response.write(t.render(c))
    return response

Then the output csv is not empty. So I guess there's something wrong when import CSV_data from views.py.

The problem is I've tested that CSV_data value in views is correct. So what could go wrong?

****************UPDATE****************:

original code in views.py is like:

 CSV_data = []
 def part_usage_result(request):
     ...(details omit)
     usageDictWeek = helper.getResultByWeek(modelName, spareCode, start, end) #returns a list
     CSV_data=usageDictWeek

I change to:

 CSV_data = []
 def part_usage_result(request):
     ...(details omit)
     usageDictWeek = helper.getResultByWeek(modelName, spareCode, start, end) #returns a list
     for each in usageDictWeek:
         CSV_data.append(each)

Now the content of csv is correct. Still don't know why this happens

2
  • 1
    you have to go deeper :) what is relevant code for helper.getResultByWeek? how is part_usage_result invoked? does CSV_data[:] = usageDictWeek help?
    – alko
    Nov 27, 2013 at 20:44
  • Yes CSV_data[:] = usageDictWeek works! I guess I need to learn more Python :)
    – alexZ
    Nov 27, 2013 at 21:02

2 Answers 2

1

As you didn't provide helper.getResultByWeek details and how it is called, I guess it returns a global variable with a list value, and this variable is modified somewhere in between.

 CSV_data = usageDictWeek

do not copy a list, but creates another reference to existing one. When later original usageDictWeek is modified, CSV_data is modified as well.

When you do instead

 CSV_data[:] = usageDictWeek 

a new copy of usageDictWeek is created and assigned to CSV_data.

0
0

try keyword content_type, instead of mimetype

1
  • Still the same. I did some change in views.py and the csv is not empty now. Please see update in question
    – alexZ
    Nov 27, 2013 at 20:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.