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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With