django variable of one view to another from session -
i m confused on , dont have idea how this.. have view have listed news news table. display news have passed context data in list view. here view
class singlenewsview(listview): model = news form_class = searchform template_name = "single_news.html" # def post(self, request, **kwargs): # print "request" # form = searchform(request.user) def get(self, request, pk, **kwargs): #form = searchform(request.user) 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 if self.pub_from , self.pub_to , 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
and have written view referenced django doc download news in csv format.. have included search form filter news. in first view have passed context["something"] display list of news in template. want download news in csv. have written view this
def csvview(request): response = httpresponse(content_type="text/csv") response["content-disposition"] = 'attachment; filename=somefilename.csv"' some_val = request.session["something"] print some_val print "this" writer = csv.writer(response) writer.writerow(some_val) return response
this next view download csv. here trying download news come after filter. in first view context["something "] gives list of news. did dont know how it. lastly m trying value of contxt["something"] session failed in too. how can value of 1 view another. or have better idea how can download news returned context["something"]. doing wrong.
setting data in context
not put in session. need set data in session store there. also, storing objects directly in session not idea. may have serialize them.
better way create list of pk
s of objects want session.
something like:
def get_context_data(self, **kwargs): context = super(singlenewsview,self).get_context_data(**kwargs) context["form"] = searchform if self.pub_from , self.pub_to , self.crawlers: qs = news.objects.filter(category_id=self.pk).filter(published_date__range=(self.pub_from,self.pub_to), crawler=self.crawlers) else: qs = news.objects.filter(category_id=self.pk) context["something"] = qs #set data in session self.request.session['something_pks'] = [ i.pk in qs ] return context
then in csvview
can them `request.session['something_pks'] , query objects.
Comments
Post a Comment