Django Pass Multiple Models to one Template

前端 未结 3 1033
庸人自扰
庸人自扰 2020-11-30 18:20

I am building an address book that includes the relationships between entries, etc. I have separate models for Individuals, Companies, Venues, and Roles. On my index page I

3条回答
  •  难免孤独
    2020-11-30 18:47

    I ended up modifying @thikonom 's answer to use class-based views:

    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
    

    and in my urls.py

    url(r'^$', 
        IndexView.as_view(),
        name="home_list"
            ),
    

提交回复
热议问题