Hellington
Hellington

Reputation: 13

Django - Listing many different models in one page

I need to list different models in a single page/url.

#models.py
class Service(models.Model):
  author = models.ForeignKey(User, related_name="services")
  title = models.CharField(max_length=255)
  slug = models.SlugField(max_length=255, unique=True)

  objects = ServiceQuerySet.as_manager()

class Carousel(models.Model):
  author = models.ForeignKey(User, related_name="carousels")
  title = models.CharField(max_length=255)
  content = models.TextField()

  objects = CarouselQuerySet.as_manager()

This is my views, this way are listed in different pages, I tried to join the queryset, but got no success.

#views.py

class ServiceListView(generic.ListView):
   model = models.Service
   queryset = models.Service.objects.published()

class CarouselListView(generic.ListView):
   model = models.Carousel
   queryset = models.Carousel.objects.published()

This is my urls.py, this listing only those services.

urlpatterns = patterns('',
  url(r'^$', views.ServiceListView.as_view(), name="service_list"),
  url(r'^$', views.CarouselListView.as_view(), name="carousel_list"),
)

I need the two lists appear on the same page. How can I accomplish this task?

Upvotes: 1

Views: 70

Answers (1)

cor
cor

Reputation: 3393

What about passing it through the context?

from .models import Service,Carousel

class ServiceListView(generic.ListView):
    model = Service
    queryset = Service.objects.published()

    def get_context_data(self, **kwargs):
        context = super(ServiceListView, self).get_context_data(**kwargs)
        context['carousel_list'] = Carousel.objects.published()
        return context

Upvotes: 1

Related Questions