django输出空csv

时间:2013-11-27 20:02:41

标签: python django csv

我正在使用django而我正在尝试将CS​​V_data列表导出到csv文件中。下面是我的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是views.py中的一个变量,我试图在模板中打印它,值没问题。

 [(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])]

但是,生成的csv始终为空。

我尝试将CS​​V_data定义添加到csv.py文件中,如下所示:

#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

然后输出csv不为空。因此,我认为从views.py导入CSV_data时出现了问题。

问题是我已经测试了视图中的CSV_data值是否正确。那可能出现什么问题?

** * ** * ** * 的** * ** * * 更新 * ** * ** * ** * ** * ** * :< / p> views.py中的

原始代码如下:

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

我改为:

 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)

现在csv的内容是正确的。 仍然不知道为什么会发生这种情况

2 个答案:

答案 0 :(得分:1)

由于您没有提供helper.getResultByWeek详细信息以及如何调用它,我猜它会返回一个带有列表值的全局变量,并且此变量会在两者之间的某处进行修改。

 CSV_data = usageDictWeek

不要复制列表,而是创建对现有列表的另一个引用。修改后期原始usageDictWeek时,也会修改CSV_data。

当您改为

 CSV_data[:] = usageDictWeek 

创建usageDictWeek的新副本并将其分配给CSV_data。

答案 1 :(得分:0)

尝试关键字content_type,而不是mimetype