Python 3, Django 1.9
It might be easier and less troublesome to write in Unicode to StringIO first and then convert to CP932 all at once.
class CSVView(View):
def get(self, request, **kwargs):
response = HttpResponse(content_type='text/csv; charset=Shift-JIS')
response['Content-Disposition'] = \
'attachment; filename=items.csv'
sio = io.StringIO()
writer = csv.writer(sio)
writer.writerow(self.header)
for row in self.get_rows():
writer.writerow(row)
response.write(sio.getvalue().encode('cp932'))
return response
Comments