0

Here is my view

class SingleNewsView(ListView):
model = News
form_class = SearchForm
template_name = "single_news.html"
def get(self, request, pk, **kwargs):
 self.pk = pk
 self.pub_from = request.GET.get('pub_date_from',False)
 self.pub_to = request.GET.get('pub_date_to',False)
 self.crawlers = request.GET.get('crawler',False)
 print self.crawlers
 return super(SingleNewsView,self).get(request,pk, **kwargs)
def get_context_data(self, **kwargs):
 context = super(SingleNewsView,self).get_context_data(**kwargs)
 context["form"] = SearchForm#(self.request.GET)
 if self.pub_from and self.pub_to and self.crawlers:
 context["something"] = News.objects.filter(category_id=self.pk).filter(published_date__range=(self.pub_from,self.pub_to), crawler=self.crawlers)
 else:
 context["something"] = News.objects.filter(category_id=self.pk)
 return context

I want to download the news as csv. When I click "Download CSV" on the news listing page I want to download the query that came after filter. How can I do that?? Any help??

asked Jun 23, 2014 at 10:05

3 Answers 3

1

Django has its own csv library. If that doesn't suit what you're looking for maybe check out django-data-export. Good luck and hope this helps!

answered Jun 23, 2014 at 14:36
Sign up to request clarification or add additional context in comments.

Comments

0

I use xlwt to export models to excel, I think this code can FYI

from xlwt import Workbook
def get(self, request, *args, **kwargs):
 book = Workbook(encoding='utf-8')
 // fill book with your data here...
 response = HttpResponse(content_type='application/ms-excel')
 book.save(response)
 response['Content-Disposition'] = 'attachment; filename="%s"' % self.excel_file_name.encode("utf-8")
 response['Cache-Control'] = 'no-cache'
 return response
answered Jun 23, 2014 at 18:09

3 Comments

I have written this method. This is not what I looked. I want to download the query given by context["something"] because there I have filtered by pk and other get data
I think you may call self.get_context_data to get the data from context["something"] and then fill the Workbook as you wish.
and how can I call context["something"]??
0

I found the following code snippet from the django docs helpful:

import csv
from django.http import HttpResponse
def some_view(request):
 # Create the HttpResponse object with the appropriate CSV header.
 response = HttpResponse(content_type='text/csv')
 response['Content-Disposition'] = 'attachment; filename="somefilename.csv"'
 writer = csv.writer(response)
 writer.writerow(['First row', 'Foo', 'Bar', 'Baz'])
 writer.writerow(['Second row', 'A', 'B', 'C', '"Testing"', "Here's a quote"])
 return response
answered Nov 15, 2015 at 21:20

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.