Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
451 views
in Technique[技术] by (71.8m points)

django - 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
question from:https://stackoverflow.com/questions/65936569/pass-get-queryset-in-context

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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))

    querySet = self.get_queryset()

    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Т.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...