简体   繁体   中英

Pass get_queryset in Context

Need help on passing the returned value from get_queryset to the context for rendering, I am reading the Django documentation on Class-based Views and I keep getting the same error name 'querySet' is not defined , any insight on what I'm doing wrong would be appreciated.

class SearchPropertyListView(ListView):
template_name = "search/search_list.html"


def get_queryset(self):

    querySet = Property.objects.all()
    city_or_neighborhood = self.request.GET.get('city_or_neighborhood')
    category = self.request.GET.get('category')
    purpose = self.request.GET.get('purpose')

    if city_or_neighborhood != '' and city_or_neighborhood is not None:
        querySet = querySet.filter(Q(city__title__icontains=city_or_neighborhood)
                                   | Q(neighborhood__title__icontains=city_or_neighborhood)
                                   ).distinct()
    elif category != '' and category is not None:
        querySet = querySet.filter(category__title=category)

    elif purpose != '' and purpose is not None:
        querySet = querySet.filter(purpose__title=purpose)

    return querySet


def  get_context_data(self, *args, **kwargs):

    context = super().get_context_data(**kwargs)
    city = City.objects.all().annotate(
        num_property=Count("property")).order_by("-num_property")
    categories = Category.objects.all()
    purposes = Purpose.objects.all()

    featured = list(Property.objects.filter(featured=True))
    shuffle(featured)
    
    context['city'] = city
    context['categories'] = categories
    context['featured'] = featured
    context['purposes'] = purposes
    context['querySet'] = querySet
    return context

In your get_context_data() , you have querySet which is not defined:

def  get_context_data(self, *args, **kwargs):
    .....
    context['purposes'] = purposes
    context['querySet'] = querySet     <----- Here
    return context

So, you should define your querySet like:

def  get_context_data(self, *args, **kwargs):

    context = super().get_context_data(**kwargs)
    city = City.objects.all().annotate(
        num_property=Count("property")).order_by("-num_property")
    categories = Category.objects.all()
    purposes = Purpose.objects.all()

    featured = list(Property.objects.filter(featured=True))

    

    shuffle(featured)
    
    context['city'] = city
    context['categories'] = categories
    context['featured'] = featured
    context['purposes'] = purposes
    context['querySet'] = querySet
    return context

*Note:- If you are expecting querySet from get_queryset() , then know that these are two different methods and you defined querySet locally. Or, you have to get that from self.get_queryset() as mentioned by @OlegТ.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM