Monday, October 3, 2011

Re: Return a CSV and a template in the same response.

On 10/03/11 17:53, Jack wrote:
> Hey gang, I am trying to use a response object to serve a
> dynamically created CSV. I have that part working perfectly
> but I'd like to display a page afterwards with a summary of
> the data. Is there a good way to do this? I tired added the
> rendered template to the httpresponse object after I write the
> CSV into it but it gives a header error.

Depending on what you're trying to return, there are a couple
options. In your particular case, I'd swap the order, returning
the page-content with a redirect points to the file-download URL.
This could be an HTML <meta> redirect, JavaScript, or an
<iframe> that points to the CSV file (http://site/csv/url/ for
this example) for download:

HTML: <meta http-equiv="refresh"
content="3;url=http://site/csv/url/" >
Javascript: location.href = 'http://site/csv/url/';
iframe: <iframe src="http://site/csv/url/">

You might also be able to (ab)use an HTML redirect to return the
page while simultaneously redirecting with a 302/307 that would
bounce the user to the given page.

If the use-case was different and you actually wanted to return
multiple files, I've used Python's zipfile module to zip up the
contents and then return the collection (in my case, returning
multiple .csv files).

-tkc


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to django-users+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/django-users?hl=en.

No comments:

Post a Comment