2010-01-04 188 views

回答

6

這裏有一個簡單的方法:

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 
1

你可以只寫其輸出逗號分隔值的模板。這非常簡單。例如,如果foo_list在上下文中:

{% for foo in foo_list %}"{{ foo.stuff }}","{{ foo.more_stuff }}" 
{% endfor %} 
+0

如何輸出列標籤? – 2017-11-02 13:56:32