简体   繁体   中英

Python Django unhashable type slice error while trying to paginate class based 'Category' list view

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: 在此处输入图像描述

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

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

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