0
這是我的看法,根據過濾器在那裏我有新聞列表...Django的繼承上下文變量
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
而現在我在這裏有以下載從上下文查詢的新聞列表[「東西」] 。
def CSVView(request):
response = HttpResponse(content_type="text/csv")
response["Content-Disposition"] = 'attachment; filename=somefilename.csv"'
writer = csv.writer(response)
writer.writerow([context["something"])# Here what can I write...
return response
現在在writer.writerow的def CSVView中,我希望上下文[「something」]形成第一個視圖。我不知道該怎麼做。我想用csv下載新聞。任何人有更好的主意?我做錯了什麼?
我試過def CSVView(請求,SingleNewsView):但它聲稱def CSVView只需要2爭論1給出的錯誤。我應該做些什麼。我怎樣才能以csv格式下載新聞?
看來你是失去了一些東西,如果你想'傳遞'從一個視圖到另一個視圖,使用會話https://docs.djangoproject.com/en/dev/topics/http/sessions/ – trinchet
我可以做some_val = request.session [context [「something」 ]? – Aaeronn
我做了some_val = request.session.get(「something」)並打印some_val。但它打印假。這是爲什麼 ??我無法獲得上下文的價值[「某事」] – Aaeronn