Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

250
Visualizações
Django how to get multiple context_object_name for multiple queryset from single view to single template

I want to run multiple queryset from single view. I have done for single get_queryset and single context_object_name to pass into index.html template :

class IndexView(generic.ListView):
    template_name = 'doorstep/index.html'
    context_object_name = 'all_hotels'

    def get_queryset(self):
        return Hotel.objects.all().order_by('rating').reverse()[:3]

Now, i need to run this queryset Hotel.objects.all().order_by('star').reverse()[:3] from same IndexView and pass context_object_name from this querset to same template_name.

I get the value as {% for hotel in all_hotels %} in the template

over 4 years ago · Santiago Trujillo
1 Respostas
Responde à pergunta

0

Override get_context_data and add any additional querysets to the context.

class IndexView(generic.ListView):
    template_name = 'doorstep/index.html'
    context_object_name = 'all_hotels'

    def get_queryset(self):
        return Hotel.objects.all().order_by('rating').reverse()[:3]

    def get_context_data(self, **kwargs):
        context = super(IndexView, self).get_context_data(**kwargs)
        context['star_hotels'] = Hotel.objects.all().order_by('star').reverse()[:3]
        # Add any other variables to the context here
        ...
        return context

You can now access {{ star_hotels }} in your template.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda