1

The desired outcome is to export a spreadsheet of all users in the database, but rather than export them all at once - break up the users into 3 groups and export each of these groups as a spread sheet.

I am using xlsxwriter and StringIO

Thus far the code is returning only the first HttpResponse object out of the 3 (aka the first chunk of users). I tried using StreamingHttpResponse but believe I misapplied it/it wasn't appropriate for this task.

The question/s: Can I return multiple spreadsheets in one response? If not, would the best thing to do be to call this function multiple times, once for each user group? (edited after first comment)

Thanks Much!

Code below:

def export_users(request):
# all users
users = User.objects.all()
# breaks up all users into 3 groups
user_groups = [users[x:x+2] for x in xrange(0, len(users), 2)]

#  FUNCTIONALITY NOTES --> idea is to split number of users for exporting into groups of 3 then export each group. right now it stops after the first group


# list var for storing all responses
full_response = []

for group in user_groups:
    response = HttpResponse(content_type='application/vnd.ms-excel')
    response['Content-Disposition'] = 'attachment; filename=UserReport.xlsx'

    # user group
    print "group! ---> %s"%(group)

    # creates data variable to write to response
    xlsx_data = WriteToExcel(group)
    response.write(xlsx_data)

    # appending each response to an array
    full_response.append(response)
    print len(full_response)

# all response objects 
print full_response

# returning one here 
return response

# non-functioning attempt to return all responses

# for response in full_response:
#     print response
#     return response
dgg
  • 333
  • 3
  • 11
  • You can return exactly one response from your view. If you need multiple responses, you also need to make multiple requests from the browser. – knbk Aug 24 '16 at 21:51
  • 1
    You can not do that. As a work around you may save the file on you static folder and the return the path of all the files. On receiving the response, client may open (or, download) all the files – Moinuddin Quadri Aug 24 '16 at 22:51
  • @MoinuddinQuadri - Sounds promising! Unfamiliar with saving files to a static folder: could you elaborate possibly as an answer to the question? – dgg Aug 24 '16 at 22:58
  • You have to configure `static` file settings within your `django` project and save the file in static folder and then return the path to that file. You will easiliy find various examples online on how to achieve that – Moinuddin Quadri Aug 24 '16 at 23:02

2 Answers2

4

You can't return multiple HttpResponse objects, but you can return multiple files in one HttpResponse if you combine them to one zipped file.

How to return multiple files in HttpResponse Django

nodarai
  • 65
  • 8
1

You can not return multiple HttpResponse object with single API call. However as a workaround, you can:

  • Approach 1: If content is going to be always dynamic and file reference is not needed in future: Make multiple request each returning HttpResponse

  • Approach 2: If content is going to be static, and you may need the same file in future: Make a single request, upload the file to aws s3, google cloud storage, or on your server. Return the path of each file with response and download those file at client side.

Moinuddin Quadri
  • 46,825
  • 13
  • 96
  • 126