среда, 20 февраля 2013 г.

Django Создание скачиваемого CSV файла

views.py

import csv

data = [146, 184, 235, 200, 226, 251, 299, 273, 281, 304, 203]

def get_csv(request):
    # Create the HttpResponse object with the appropriate CSV header.
    response = HttpResponse(mimetype = 'text/csv')
    response['Content-Disposition'] = 'attachment; filename = data.csv'   
    # Create the CSV writer using the HttpResponse as the "file."
    writer = csv.writer(response)
    writer.writerow(['Year', 'Airline Passengers'])
    for (year, num) in zip(range(1995, 2006), data):
        writer.writerow([year, num])  
    return response

Комментариев нет:

Отправить комментарий