开发者

Problem exporting an xls file for user download with django and HttpResponse

I'm currently creating a spreadsheet using xlwt and trying to export it out as an HttpResponse in django for a user to download. My code looks like this:

response = HttpResponse(mimetype = "application/vnd.ms-excel")
response['Content-Disposition'] = 'attachment; filename = %s +".xls"' % u'Zinnia_Entries'
work_book.save(response)
return response

Which seems to be the right way to do it, but I'm getting a:

Traceback (most recent call last):
  File "C:\dev\workspace-warranty\imcom\imcom\wsgiserver.py", line 1233, in communicate
    req.respond()
  File "C:\dev\workspace-warranty\imcom\imcom\wsgiserver.py", line 745, in respond
    self.server.gateway(self).respond()
  File "C:\dev\workspace-warranty\imcom\imcom\wsgiserver开发者_如何转开发.py", line 1927, in respond
    response = self.req.server.wsgi_app(self.env, self.start_response)
  File "C:\dev\workspace-warranty\3rdparty\django\core\servers\basehttp.py", line 674, in __call__
    return self.application(environ, start_response)
  File "C:\dev\workspace-warranty\3rdparty\django\core\handlers\wsgi.py", line 252, in __call__
    response = middleware_method(request, response)
  File "C:\dev\workspace-warranty\imcom\imcom\seo_mod\middleware.py", line 33, in process_response
    response.content = strip_spaces_between_tags(response.content.strip())
  File "C:\dev\workspace-warranty\3rdparty\django\utils\functional.py", line 259, in wrapper
    return func(*args, **kwargs)
  File "C:\dev\workspace-warranty\3rdparty\django\utils\html.py", line 89, in strip_spaces_between_tags
    return re.sub(r'>\s+<', '><', force_unicode(value))
  File "C:\dev\workspace-warranty\3rdparty\django\utils\encoding.py", line 88, in force_unicode
    raise DjangoUnicodeDecodeError(s, *e.args)
DjangoUnicodeDecodeError: 'utf8' codec can't decode byte 0xd0 in position 0: invalid continuation byte. You passed in 

(I left off the rest because I get a really long line of this \xd0\xcf\x11\xe0\xa1\xb1\x1a\xe1\x00 kind of stuff)

Do you guys have any ideas on something that could be wrong with this? Is is it because some of my write values look like this:

work_sheet.write(r,#,information) where information isn't cast to unicode?


response['Content-Disposition'] = 'attachment; filename = %s +".xls"' % u'Zinnia_Entries'

should just be

response['Content-Disposition'] = 'attachment; filename = %s.xls' % u'Zinnia_Entries'

without quotes around .xls otherwise the output will be

u'attachment; filename = Zinnia_Entries +".xls"'

So try changing that.

But also check out this answer. It has a really helpful little function for outputing xls files.

django excel xlwt


Solved the problem. Apparently someone had put some funky middleware stuff in that was hacking apart and appending and adding, ect. ect. to the file. When it shouldn't.

Anyway, with it gone the file exports perfectly.

@Storm - Thank you for all the help!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜