Bootstrap

Django-生成csv文件

定义:

python中生成csv文件:

 csv文件下载:

事例:

 例子:

urls:

from django.urls import path
from . import views
urlpatterns = [

    path('test_csv',views.test_csv),
]

views:

def test_csv(request):
    response = HttpResponse(content_type='text/csv')
    response['Content-Disposition'] = 'attachment; filename="test.csv"'
    writer = csv.writer(response)
    writer.writerow(['Title', 'Content'])
    for i in range(100):
        writer.writerow([i, f'Content {i}'])
    return response

;