1
votes

Is there an easy way to do the following?

after getting the request in the view, send back to the user a file, plus a re-rendered template of the page? something like "merging" reponse with a file and render_to_response

This is how I return a response with a file:

filename = "/path/to/somewhere"
wrapper  = FileWrapper(open(filename))
content_type = mimetypes.guess_type(filename)[0]
response = HttpResponse(wrapper,content_type=content_type)
response['Content-Length'] = os.path.getsize(filename)
response['Content-Disposition'] = "attachment; filename=%s"%filename
return response

This is how I return an ordinary template with rendered data:

data = getData()
return render_to_response('../templates/some_template.html', {'data': data,})

(I might have dropped some important lines with copy-paste, but to make my point - this code works, the problem is not with these two code samples)

the question is: how do I "merge" both of them together? is there a simple way to do this with django standard functionality? Do I have to use Ajax for this? (I'm not familiar with ajax... so if there's a way to do this without, it's preferable)

Thanks,

2
I can't imagine what you want the user to see/get here. How would this even work? - Daniel Roseman
What you are asking for are two contents within the same response? There's no easy way to do that. You should probably go with Ajax. - Pedro Werneck
Daniel - I have a button on the page, which I want it to do 2 jobs at once - send a file back to the user, and update a "div" section on the page with new data. maybe what I was suggesting here (using 2 request at once) is not the solution, but any idea on how to get the right user-experience is welcomed - nivniv

2 Answers

4
votes

If you simplify your view of the Hypertext Transfer Protocol, the way your users interact with your application is by sending a request for which your application provides a response. In section 4.2 of RFC 6266, when you return a response that matches the "attachment" disposition type, your instructing the user's client that it should "prompt the user to save the response locally, rather than process it normally".

There's no support for serving a response that contains HTML and some other content type and which can instruct the client to save one and display the other. The fact that it isn't supported doesn't mean that you shouldn't explore for alternatives to provide such a user experience, but it should hint that we're not practicing it and the user wouldn't expect it.

But, facts aside, if you really want to do it you need to understand that serving the file and displaying a new HTML document is going to take two separate request/response contexts. The simplest design would include that page from which the user is going to initiate the request to retrieve the file also subscribes with the application to be notified when the user has retrieved the file in completion and then goes off to display some new content or redirect to a new page.

0
votes

It's possible using multipart.

The HttpResponse is subclassed and enables returning multipart data.

At the client side you need to receive the data in chunks and parse according to the appropriate Mime Type.

Here is an implementation

https://anentropic.wordpress.com/2010/05/14/jsdjango-streaming-multi-part-ajax-responses-mxhr/