Re: How to download the table generated in html as a file
On Mon, Jul 8, 2013 at 10:30 AM, sahitya pavurala
<sahitya.pavurala@gmail.com> wrote:
>
> Hi ,
>
>
> I am new to Django and I would be glad if i can get help on this .
>
> I am generating a html page where i get a table containing fields and values
> of a database .
> I want to download this table as a file (preferably as an excel file ) .
> How can i do this ?
I use the python csv lib (http://docs.python.org/2/library/csv.html)
and do something like this:
def download_table(self, request):
from django.http import HttpResponse
from datetime import date
import csv
response = HttpResponse(mimetype='text/csv')
response['Content-disposition'] = "attachment;
filename=%s_%s_%s.csv" % (
request.user.username.lower(),
self.report.name,
date.today())
writer = csv.writer(response, dialect=csv.excel)
data = self.get_data()
rows = []
rows.append(self.get_headers())
rows += data
for row in rows:
writer.writerow(row)
return response
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users.
For more options, visit https://groups.google.com/groups/opt_out.
0 Comments:
Post a Comment
Subscribe to Post Comments [Atom]
<< Home