41

我正在构建一个地址簿,其中包含条目之间的关系等。我为个人、公司、地点和角色设置了单独的模型。在我的索引页面上,我想列出每个模型的所有实例,然后过滤它们。这样人们就可以轻松搜索并找到条目。我已经能够使用通用视图列出一个模型并使用 get_extra_context 来显示另一个模型:

#views.py

 class IndividualListView(ListView):

    context_object_name = "individual_list"
    queryset = Individual.objects.all()
    template_name='contacts/individuals/individual_list.html'


class IndividualDetailView(DetailView):

    context_object_name = 'individual_detail'
    queryset = Individual.objects.all()
    template_name='contacts/individuals/individual_details.html'

    def get_context_data(self, **kwargs):
        context = super(IndividualDetailView, self).get_context_data(**kwargs)
        context['role'] = Role.objects.all()
        return context

我还可以使用自定义视图列出单个模型:

#views.py
def object_list(request, model):
    obj_list = model.objects.all()
    template_name = 'contacts/index.html'
    return render_to_response(template_name, {'object_list': obj_list}) 

以下是这两个测试的 urls.py:

(r'^$', views.object_list, {'model' : models.Individual}),

(r'^individuals/$', 
    IndividualListView.as_view(),
        ),
(r'^individuals/(?P<pk>\d+)/$',
    IndividualDetailView.as_view(),

         ),

所以我的问题是“如何修改它以将多个模型传递给模板?” 甚至可能吗?StackOverflow 上的所有类似问题只询问两个模型(可以使用 get_extra_context 解决)。

4

3 回答 3

66

我最终修改了@thikonom 的答案以使用基于类的视图:

class IndexView(ListView):
    context_object_name = 'home_list'    
    template_name = 'contacts/index.html'
    queryset = Individual.objects.all()

    def get_context_data(self, **kwargs):
        context = super(IndexView, self).get_context_data(**kwargs)
        context['roles'] = Role.objects.all()
        context['venue_list'] = Venue.objects.all()
        context['festival_list'] = Festival.objects.all()
        # And so on for more models
        return context

在我的 urls.py

url(r'^$', 
    IndexView.as_view(),
    name="home_list"
        ),
于 2012-09-10T18:31:57.217 回答
24

我建议你删除你的object_list观点,

为此特定视图定义字典,

   all_models_dict = {
        "template_name": "contacts/index.html",
        "queryset": Individual.objects.all(),
        "extra_context" : {"role_list" : Role.objects.all(),
                           "venue_list": Venue.objects.all(),
                           #and so on for all the desired models...
                           }
    }

然后在您的网址中:

#add this import to the top  
from django.views.generic import list_detail

(r'^$', list_detail.object_list, all_models_dict),
于 2012-08-29T23:22:57.063 回答
9

如果你想在 Django 1.5 上构建它,你将能够使用稳定版本的 CBV。请在下面找到代码。

你可以在这里找到很棒的文档https://docs.djangoproject.com/en/dev/topics/class-based-views/mixins/

class ProductsCategoryList(ListView):
    context_object_name = 'products_list'
    template_name = 'gallery/index_newborn.html'

    def get_queryset(self):
        self.category = get_object_or_404(Category, name=self.args[0])
        return Products.objects.filter(category=self.category)

    def get_context_data(self, **kwargs):
        kwargs['category'] = Category.objects.all()
        # And so on for more models
        return super(ProductsCategoryList, self).get_context_data(**kwargs)
于 2013-05-18T13:43:53.230 回答