标签:djang created type ade name header csv iter tac
import csv from datetime import timedelta def download_csv(commend=None): response = HttpResponse(content_type=‘text/csv‘) response[‘Content-Disposition‘] = ‘attachment;filename=download.csv‘ response.write(‘\xEF\xBB\xBF‘) writer = csv.writer(response) headers = [u"姓名",u"年龄",u"性别"] headers = [item.encode(‘utf8‘) for item in headers] writer.writerow(headers) list_user = User.object.filter(**commend).order_by("-datetime_created") queryset = list_user.values_list("user__username","age","sex") for idx,row in enumerate(queryset): values = [] cnt = 0 for field in row: cnt += 1 value = ‘‘ if cnt == 2: if int(field) == 0: value = u‘男‘ elif int(field) == 1: value = u‘女‘ elif field: if type(field) is datetime.datetime or type(field) is datetime.date: field = field + timedelta(hours=8) value = field.strftime("%Y-%m-%d") else: value = field values.append(unicode(value).encode(‘utf-8‘)) writer.writerow(values) return response
标签:djang created type ade name header csv iter tac
原文地址:http://www.cnblogs.com/bk770466199/p/7909790.html