Open Kaito-a-bit opened 2 years ago
def csv_export(request):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="books.csv"'
writer = csv.writer(response)
for book in Book.objects.all():
row = [author for author in book.objects.all()]
for author in book.coauthors.all():
row.append(author.username)
strAuthors = " ".join(row)
writer.writerow([book.id, book.name, book.publisher.name, strAuthors, book.published_date])
return response
短くする
def csv_export(request):
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="books.csv"'
writer = csv.writer(response)
for book in Book.objects.all():
strAuthors = " ".join([author.username for author in book.coauthors.all()])
writer.writerow([book.id, book.name, book.publisher.name, strAuthors, book.published_date])
return response
現状
book/book_list.html
に変更これから
exportボタン配置