Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7645747
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T10:01:16+00:00 2026-05-31T10:01:16+00:00

I have a django view that I want to return an Excel file. The

  • 0

I have a django view that I want to return an Excel file. The code is below:

def get_template(request, spec_pk):
    spec = get_object_or_404(Spec, pk=spec_pk)

    response = HttpResponse(spec.get_template(), mimetype='application/ms-excel')
    response['Content-Disposition'] = 'attachment; filename=%s_template.xls' % spec.name
    return response

In that example, the type of spec.get_template() is <type 'bytearray'> which contains the binary data of an Excel spreadsheet.

The problem is, when I try to download that view, and open it with Excel, it comes in as garbled binary data. I know that the bytearray is correct though, because if I do the following:

f = open('temp.xls', 'wb')
f.write(spec.get_template())

I can open temp.xls in the Excel perfectly.

I’ve even gone so far as to modify my view to:

def get_template(request, spec_pk):
    spec = get_object_or_404(Spec, pk=spec_pk)
    f = open('/home/user/temp.xls', 'wb')
    f.write(spec.get_template())
    f.close()

    f = open('/home/user/temp.xls', 'rb')
    response = HttpResponse(f.read(), mimetype='application/ms-excel')
    response['Content-Disposition'] = 'attachment; filename=%s_template.xls' % spec.name
    return response

And it works perfectly- I can open the xls file from the browser into Excel and everything is alright.

So my question is- what do I need to do that bytearray before I pass it to the HttpResponse. Why does saving it as binary, then re-opening it work perfectly, but passing the bytearray itself results in garbled data?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T10:01:17+00:00Added an answer on May 31, 2026 at 10:01 am

    Okay, through completely random (and very persistent) trial and error, I found a solution using the python binascii module.

    This works:

    response = HttpResponse(binascii.a2b_qp(spec.get_template()), mimetype='application/ms-excel')
    

    According to the python docs for binascii.a2b_qp:

    Convert a block of quoted-printable data back to binary and return the binary data. More than one line may be passed at a time. If the optional argument header is present and true, underscores will be decoded as spaces.

    Would love for someone to tell me why saving it as binary, then reopening it worked though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a django view that returns HTTP 301 on a curl request: grapefruit:~
I have a view in a Django 1.4 project: def index(request): print reverse('menus_index') latest_menu_list
I have a simple Django view that just returns URL parameters, but if I
I have the view function in django that written like a dispatcher calling other
I have a Django view called change_priority. I'm posting a request to this view
I have a Django view that receives POSTs which do not need to have
Still new to python and django, though learning ;-) I have a view that
I have a model that I want to save in Django Admin class Product(models.Model):
I am developing my first django website. I have written code in my view
The following in part of my view file def user_detail(request, username, template_name='auth/user_detail.html', extra_context=None): context

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.