Home > Back-end >  Python Django unhashable type slice error while trying to paginate class based 'Category'
Python Django unhashable type slice error while trying to paginate class based 'Category'

Time:06-19

I am trying to create a Category page that filters out posts by its category, everything is fine till I try to add pagination option to its class in views.py. Here is the error: enter image description here

This is what I have in views.py:

class CatListView(ListView):
    template_name = 'academy/category.html'
    context_object_name = 'catlist'
    paginate_by = 2

    def get_queryset(self):
        content = {
        'cat': self.kwargs['category'],
        'posts': ContentPost.objects.filter(category__name=self.kwargs['category'])
        }
        return content

def category_list(request):
    category = Category.objects.exclude(name='default')
    context = {
    'category' : category,
    }

    return context

and this is what I have in urls.py:

path('academy/category/<category>/', CatListView.as_view(),name='academy-category'),

I appreciate your help in advance... Thanks

CodePudding user response:

As Django documentation when you use the get_queryset method it should return Queryset that will be used to retrieve the object that this view will display. https://docs.djangoproject.com/en/4.0/ref/class-based-views/mixins-single-object/#django.views.generic.detail.SingleObjectMixin.get_queryset

the query is wrong you must specify what you want from filter name like add __icontains or __contains

    def get_queryset(self):
        return ContentPost.objects.filter(category__name__icontains=self.kwargs['category'])
  • Related