简体   繁体   English

如何将查询集或上下文字典从一个视图传递到另一个视图

[英]How to pass querysets or the context dictronary from one view to another view

I need to pass a queryset from one view to another.我需要将查询集从一个视图传递到另一个视图。 I read I could use sessions to archive this, but I have no clou how this works.我读到我可以使用会话来存档它,但我不知道它是如何工作的。 In my first view, I filter by some values and save the values in a variable (type : queryset).在我的第一个视图中,我按一些值进行过滤并将这些值保存在一个变量中(类型:queryset)。 I need this queryset in another view to display the results on another template.我需要在另一个视图中使用此查询集以在另一个模板上显示结果。

I would be appreciate for any bit of help.如果能提供任何帮助,我将不胜感激。

You can create a queryset in a view and save it in the session so that you can use same queryset results in some other view.您可以在视图中创建查询集并将其保存在会话中,以便您可以在其他视图中使用相同的查询集结果。 But i am not sure why you need to do this.但我不确定你为什么需要这样做。 Here is an example.这是一个例子。

def first_view(request):
   cards = Card.objects.all()
   request.session['cards'] = cards

def second_view(request):
   cards = request.session['cards']
   # you can use the cards queryset to render in a template

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM