会话的一个视图到另一个视图的django变量 [英] django variable of one view to another from session
问题描述
我对此很困惑,我也不知道该怎么做.我有一个视图,其中列出了新闻列表中的所有新闻.为了显示新闻,我已经在列表视图中传递了上下文数据.这是我的观点
I m very confused on this and I dont have any idea how to do this.. I have a view where I have listed all the news from my news table. To display the news I have passed context data in list view. Here is my 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 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
并且我写了我从django doc引用的视图,以csv格式下载新闻.在我的第一个视图中,我已传递context ["something"]以显示模板中的新闻列表.现在,我想要的是在csv中下载该新闻.我为此写了一个观点
and I have written view that I referenced from django doc to download the news in csv format.. I have also included a search form to filter the news. In my first view I have passed context["something"] to display the list of news in the template. Now what I want is to download that news in csv. I have written a view for 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
这是我下载csv的下一个视图.在这里,我要尝试的是下载过滤器之后的新闻.在我的第一个视图中,context ["something"]给出了新闻列表.我做了所有,但不知道如何得到它.最后,我试图从会话中获取contxt ["something"]的值,但是我也失败了.如何将一个视图的价值带到另一个视图.或者,任何人都有更好的主意,我该如何下载由context ["something"]返回的新闻.我究竟做错了什么.
This is my next view to download the csv. Here what I am trying to do is to download the news that come after filter. In my first view context["something "] gives the list of news. I did all but dont know how to get it. Lastly I m trying to get the value of contxt["something"] from session but I am failed in that too. How can I get the value of one view to another. Or anyone have better idea how can I download the news that is returned by context["something"]. What am I doing wrong.
推荐答案
在 context
中设置数据不会将其放入会话中.您需要在会话中设置数据以将其存储在其中.同样,直接在会话中存储对象也不是一个好主意.您可能需要序列化它们.
Setting data in context
does not put it in session. You need to set data in session to store it there. Also, storing objects directly in session is not a good idea. You may have to serialize them.
更好的方法是创建要加入会话的对象的 pk
个列表.
Better way would be create list of pk
s of objects you want into session.
类似的东西:
def get_context_data(self, **kwargs):
context = super(SingleNewsView,self).get_context_data(**kwargs)
context["form"] = SearchForm
if self.pub_from and self.pub_to and 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 for i in qs ]
return context
然后在 CSVView
中,您可以通过`request.session ['something_pks']获取它们并查询对象.
Then in CSVView
you can get them with `request.session['something_pks'] and do the query for objects.
这篇关于会话的一个视图到另一个视图的django变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!