Django Pass Multiple Models to one Template
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"
),
I suggest you remove your object_list
view,
define a dictionary for this specific view,
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...
}
}
and then in your urls:
#add this import to the top
from django.views.generic import list_detail
(r'^$', list_detail.object_list, all_models_dict),