作为 Django 初学者,我正在研究 django 文档提供的教程,网址为https://docs.djangoproject.com/en/1.5/intro/tutorial04/ https://docs.djangoproject.com/en/1.5/intro/tutorial04/
在其中,他们演示了使用按发布日期查询列出的多个民意调查的列表。我可以添加另一个列表也可以在要使用的模板中使用吗?示例 在同一页面上按日期显示最新民意调查列表,并按字母顺序显示另一个民意调查列表。
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”方法来检索这些值,然后它们将在视图中可用。就像是:
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 }} 将在您的模板中可用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)