Skip to content

Instantly share code, notes, and snippets.

@jheasly
Last active August 29, 2015 14:21
Show Gist options
  • Select an option

  • Save jheasly/0f7c53eec6d92905e127 to your computer and use it in GitHub Desktop.

Select an option

Save jheasly/0f7c53eec6d92905e127 to your computer and use it in GitHub Desktop.
utf16-le encoded object_list
from django.http import HttpResponse
from django.template import RequestContext, loader
from foo.models import Baz
...
def foo(request):
queryset = Baz.objects.order_by('date')
template_name = 'my_template.html'
mimetype = 'text/plain'
t = loader.get_template(template_name)
c = RequestContext(request, {'object_list': queryset})
data = t.render(c)
data = data.encode('utf-16le')
response = HttpResponse(data, mimetype=mimetype)
response['Content-Disposition'] = 'attachment; filename=InDesign_tagged_text.txt'
return response
@sbutler
Copy link

sbutler commented May 22, 2015

I would change the mimetype to "text/plain; charset=UTF-16LE" just to play nice, but it probably will never matter with your Content-Disposition. Also, HttpResponse's mimetype parameter has been deprecated since 1.5; use content_type instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment