开发者

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 %}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜