我可以在Django generic.ListView中有多个列表吗? [英] Can I have multiple lists in a Django generic.ListView?

查看:384
本文介绍了我可以在Django generic.ListView中有多个列表吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为Django初学者,我正在django docs提供的教程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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆