Change charset of HttpResponse

667 views
Skip to first unread message

Benjamin W.

unread,
Jan 9, 2010, 11:26:08 AM1/9/10
to Django users
Hi there,

how can I change the charset of an HttpRespone?
I tried something like this:

response = HttpResponse(mimetype='text/csv')
response['Content-Disposition'] = 'attachment; filename=test.csv'
response['charset'] = 'ISO-8859-1'

But this doesn't work - I still get an UTF-8 encoded file.
Thanks a lot for your help,
greets ben

Karen Tracey

unread,
Jan 9, 2010, 12:03:07 PM1/9/10
to django...@googlegroups.com
On Sat, Jan 9, 2010 at 11:26 AM, Benjamin W. <b_w...@gmx.net> wrote:
how can I change the charset of an HttpRespone?
I tried something like this:

   response = HttpResponse(mimetype='text/csv')
   response['Content-Disposition'] = 'attachment; filename=test.csv'
   response['charset'] = 'ISO-8859-1'

But this doesn't work - I still get an UTF-8 encoded file.


You can change the charset for all responses by changing settings.DEFAULT_CHARSET.

Changing the charset for just certain responses is not at present so easy (though it can be done), see: http://code.djangoproject.com/ticket/10190.  Note if you do it manually now based on how the current code is written it may stop working when that ticket gets fixed in trunk, depending on specifics of the fix (which I have not looked at).

Karen

Benjamin W.

unread,
Jan 9, 2010, 12:53:11 PM1/9/10
to Django users
Thanks Karen, I'll take a look at this.
Reply all
Reply to author
Forward
0 new messages