我可以在 Django generic.ListView 中有多个列表吗? [英] Can I have multiple lists in a Django generic.ListView?
问题描述
作为一个 Django 初学者,我正在研究 django 文档提供的教程,位于 https://docs.djangoproject.com/en/1.5/intro/tutorial04/
As a Django beginner I'm working on the the tutorial provided by django docs at https://docs.djangoproject.com/en/1.5/intro/tutorial04/
在其中,他们展示了使用按发布日期查询列出的多个民意调查的列表.我可以添加另一个列表,也可以在要使用的模板中使用.示例 在同一页面上按日期显示最新民意调查列表,并按字母顺序显示另一个.
In it they demonstrate a list of multiple polls that are listed using a query by publication date. Could I add another list to be also used in the template to be used as well. Example Displaying a list of latest polls by date and another by alphabetical order on the same page.
class IndexView(generic.ListView):
template_name = 'polls/index.html'
context_object_name = 'latest_poll_list'
def get_queryset(self):
"""Return the last five published polls."""
return Poll.objects.order_by('-pub_date')[:5]
推荐答案
当然,您只需要编写自己的get_context_data"方法来检索这些值,然后它们将在视图中可用.类似的东西:
Absolutely, you'll just need to write your own 'get_context_data' method that will retrieve those values and then they will be available in the view. Something like:
def get_context_data(self, *args, **kwargs):
context = super(IndexView, self).get_context_data(*args, **kwargs)
context['alphabetical_poll_list'] = Poll.objects.order_by('name')[:5]
return context
有了这个 {{ latest_poll_list }} 和 {{alphabetical_poll_list }} 都可以在你的模板中使用.
With this both {{ latest_poll_list }} and {{ alphabetical_poll_list }} would be available in your template.
这篇关于我可以在 Django generic.ListView 中有多个列表吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!