'Django TemplateView is not Working with This Code

Am having issues writting Views for django oscar extended TemolateView

  class CatalogueView(TemplateView):
       """
       Browse all products in the catalogue
       """
       context_object_name = "products"
       template_name = 'oscar/catalogue/browse.html'
       model = Product

        def get(self, request, *args, **kwargs):
            try:
                self.search_handler = self.get_search_handler(
                self.request.GET, request.get_full_path(), [])
                response = super().get(request, *args, **kwargs)
            except InvalidPage:
                # Redirect to page one.
        
                messages.error(request, _('The given page number was invalid.'))
                return redirect('catalogue:index')
                return response

        def get_search_handler(self, *args, **kwargs):
                return get_product_search_handler_class()(*args, **kwargs)

        def get_context_data(self, **kwargs):
                context = super(CatalogueView,  self).get_context_data(**kwargs)
                ctx = {}
                ctx['summary'] = _("All products")
                ctx['pro_list'] = Product.objects.all()
                ctx['pro_list1'] = Product.objects.all().filter().order_by('upc')
                search_context = self.search_handler.get_search_context_data(
                self.context_object_name)
     
                ctx.update(search_context)
                return ctx

Don't mind the messy Code indentation format. Django Oscar uses the class based TemplateViews and I want to write more queresets for my products model but everything seems not working. get_context_data will not work. I need a way to add more Queresets to the TemplateView in the catalogue view. How can this be resolved? I need to filter more queresets for the Product

django django-models django-views



Solution 1:[1]

I'm not familiar with django but I guess maybe you can try:

    def get_context_data(self, **kwargs):
        context = super(CatalogueView,  self).get_context_data(**kwargs)
        context['summary'] = _("All products")
        context['pro_list'] = Product.objects.all()
        context['pro_list1'] = Product.objects.all().filter().order_by('upc')
        search_context = self.search_handler.get_search_context_data(
        self.context_object_name)

        context.update(search_context)
        return context

Remove that ctx thing.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 sillygoat