简体   繁体   English

如何在Django CBV ListView中具有多个列表?

[英]How to have multiple lists in Django CBV ListView?

In my app, I want to have an index page which will display two lists of objects, where both lists contain the same type of object (ie, same Model). 在我的应用中,我希望有一个索引页面,该页面将显示两个对象列表,其中两个列表都包含相同类型的对象(即,相同的Model)。

In traditional function based views this is easy: I define two variables, assign them to querysets, and pass them into the context to my template where I can easily access them by name. 在传统的基于函数的视图中,这很容易:我定义了两个变量,将它们分配给查询集,然后将它们传递到模板的上下文中,在这里我可以轻松地按名称访问它们。

I'm still new to CBVs and it seems there is a lot of magic, a lot of things that are handled automatically. 我还是CBV的新手,似乎有很多魔术,很多东西都是自动处理的。 I understand how I can override the queryset for a ListView (which defaults to all objects for the given Model), but what I don't get is how to supply multiple querysets, so that my ListView can actually display two lists. 我了解如何覆盖ListView的查询集(默认情况下给定Model的所有对象),但是我没有得到的是如何提供多个查询集,这样我的ListView实际上可以显示两个列表。

My only thought so far is to override self.object_list to be a tuple of two querysets, but that seems like it would make my template code less clear and I'm not even certain it would work. 到目前为止,我唯一的想法是将self.object_list覆盖为两个查询集的元组,但这似乎会使我的模板代码不太清晰,甚至不确定它是否可以工作。

If you do not want to support pagination in your multiple lists view, I would suggest overwriting the get_context_data and get methods of your view class 如果您不希望在多列表视图中支持分页,建议您覆盖视图类的get_context_data和get方法。

def get_context_data(self, **kwargs):
    """
    Get the context for this view.
    """
    queryset = kwargs.pop('object_list', self.object_list)
    queryset2 = kwargs.pop('object_list', self.object_list2)

    context = {
        'paginator': None,
        'page_obj': None,
        'is_paginated': False,
        'object_list': queryset,
        'object_list3': queryset2
    }

    context.update(kwargs)

    return context

def get(self, request, *args, **kwargs):
    self.object_list1 = self.get_queryset1()
    self.object_list2 = self.get_queryset2()

    context = self.get_context_data()
    return self.render_to_response(context)

Sorry for the names (1 and 2) but you should place more descriptive whenever I placed names like "get_queryset1" 对不起,名称(1和2),但是每当我放置“ get_queryset1”之类的名称时,都应添加更具描述性的名称

What is the difference between the items in both lists? 两个列表中的项目之间有什么区别? If you're talking about a single list (ie queryset) which can be split in two by some of their properties, this should simply be done in the template itself. 如果您要谈论的是一个列表(即查询集),可以通过其某些属性将其分成两部分,则只需在模板本身中完成即可。

For example, imagine you have a list of users, and you want to display them by gender, ie men in one list and women in the other. 例如,假设您有一个用户列表,并且想按性别显示用户,即一个列表中为男性,另一个列表中为女性。 In this case simply return a single queryset like normally with the ListView , and then in the template put something like: 在这种情况下,只需像通常使用ListView一样返回单个查询集,然后在模板中放入类似内容:

<h4>Male Users</h4>
<li>
    {% for user in users %}
    {% if not user.is_female %}<ul>{{ user.full_name }}</ul>
    {% endfor %}
</li>

<h4>Female Users</h4>
<li>
    {% for user in users %}
    {% if user.is_female %}<ul>{{ user.full_name }}</ul>
    {% endfor %}
</li>

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

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