1

I am generating .csv file in django:

if request.GET.get('format', None) == 'csv':
    # Create the HttpResponse object with the appropriate CSV header.
    response = HttpResponse(content_type='text/csv; charset=windows-1251')
    response['Content-Disposition'] = 'attachment; filename=stat.csv'

    t = loader.get_template('stat/csv.djhtml')
    c = Context({
        'data': table,
        'total': total,
    })
    response.write(t.render(c))
    return response

In csv.djhtml:

Дата/Время;Показов;Кликов;CTR;Доход/Расход
{% for item in data %}
    {{item.time}};{{item.views}};{{item.clicks}};{{item.ctr}} %;{{item.income|floatformat:2}} p.
{% endfor %}

File is generated with UTF8 encoding. How can I make this file be generated with windows-1251?

4

1 回答 1

2
...
response.write(t.render(c).encode('cp1251'))
return response
于 2012-04-08T17:14:03.590 回答