How to convert query list into CSV using django
How to convert query list into CS开发者_运维百科V using django?
Here's one simple way:
from django.http import HttpResponse
import csv
response = HttpResponse (content_type='text/csv')
writer = csv.writer(response)
# now you can use writer.writerows() to write comma separated values to your response object, for example:
from django.contrib.auth.models import User
writer.writerows(User.objects.values_list('id','username','date_joined'))
# return the response object:
return response
You can just write a template which outputs comma-separated values. It's pretty straightforward. For instance if foo_list is in the context:
{% for foo in foo_list %}"{{ foo.stuff }}","{{ foo.more_stuff }}"
{% endfor %}
精彩评论