在Django中实现分页和搜索过滤器

时间:2018-10-16 15:50:17

标签: python django filter pagination

请帮助我尝试使用搜索过滤器实现Django分页。我不断收到以下错误:

TypeError位于/ 'method'类型的对象没有len()

以下是我到目前为止的代码:

def product_list(request, category_slug=None):
    search_term = ''
    category = None
    categories = Category.objects.all()
    adds = AddBanner.objects.all()
    products_list = Product.objects.filter(available=True).order_by("-updated_at")
    productsImage = ProductImage.objects.all()

    print(product_list)

    query = request.GET.get("search")
    if query:
        products_list = products_list.filter(
        Q(name__icontains=query) |
        Q(description__icontains=query)
        ).distinct



    paginator = Paginator(products_list, 10)  # Show 10 contacts per page
    page = request.GET.get('page')

    try:
        products = paginator.page(page)
    except PageNotAnInteger:
        products = paginator.page(1)
    except EmptyPage:
        products = paginator.page(paginator.num_pages)


    context = {
        'category': category,
        'categories': categories,
        'products': products,
        'productsImages': productsImage,
        'search_term': search_term,
        'adds': adds

    }
    return render(request, 'shop/product/list.html', context)

2 个答案:

答案 0 :(得分:1)

您在此处错过了括号:

if query:
    products_list = products_list.filter(
    Q(name__icontains=query) |
    Q(description__icontains=query)
    ).distinct

应为 .distinct() 这就是为什么您会收到错误type 'method' has no len()

答案 1 :(得分:0)

    Environment:


Request Method: GET
Request URL: http://127.0.0.1:8000/cellphone-holder/?search=mini

Django Version: 2.1.1
Python Version: 3.6.6
Installed Applications:
['shop.apps.ShopConfig',
 'addBanner.apps.AddbannerConfig',
 'cart.apps.CartConfig',
 'orders.apps.OrdersConfig',
 'accounts.apps.AccountsConfig',
 'contact.apps.ContactConfig',
 'analytics.apps.AnalyticsConfig',
 'crispy_forms',
 'comments.apps.CommentsConfig',
 'recommend.apps.RecommendConfig',
 'blog.apps.BlogConfig',
 'tinymce',
 'django.contrib.admin',
 'django.contrib.auth',
 'django.contrib.contenttypes',
 'django.contrib.sessions',
 'django.contrib.messages',
 'django.contrib.staticfiles']
Installed Middleware:
['django.middleware.security.SecurityMiddleware',
 'django.contrib.sessions.middleware.SessionMiddleware',
 'django.middleware.common.CommonMiddleware',
 'django.middleware.csrf.CsrfViewMiddleware',
 'django.contrib.auth.middleware.AuthenticationMiddleware',
 'django.contrib.messages.middleware.MessageMiddleware',
 'django.middleware.clickjacking.XFrameOptionsMiddleware']



Traceback:

File "C:\Users\MMaleka\Desktop\Ideas\Ecommerece\env\lib\site-packages\django\core\paginator.py" in count
  87.             return self.object_list.count()

During handling of the above exception ('function' object has no attribute 'count'), another exception occurred:

File "C:\Users\MMaleka\Desktop\Ideas\Ecommerece\env\lib\site-packages\django\core\handlers\exception.py" in inner
  34.             response = get_response(request)

File "C:\Users\MMaleka\Desktop\Ideas\Ecommerece\env\lib\site-packages\django\core\handlers\base.py" in _get_response
  126.                 response = self.process_exception_by_middleware(e, request)

File "C:\Users\MMaleka\Desktop\Ideas\Ecommerece\env\lib\site-packages\django\core\handlers\base.py" in _get_response
  124.                 response = wrapped_callback(request, *callback_args, **callback_kwargs)

File "C:\Users\MMaleka\Desktop\Ideas\Ecommerece\src\shop\views.py" in product_list_by_category
  80.         products = paginator.get_page(page)

File "C:\Users\MMaleka\Desktop\Ideas\Ecommerece\env\lib\site-packages\django\core\paginator.py" in get_page
  63.         return self.page(number)

File "C:\Users\MMaleka\Desktop\Ideas\Ecommerece\env\lib\site-packages\django\core\paginator.py" in page
  67.         number = self.validate_number(number)

File "C:\Users\MMaleka\Desktop\Ideas\Ecommerece\env\lib\site-packages\django\core\paginator.py" in validate_number
  45.         if number > self.num_pages:

File "C:\Users\MMaleka\Desktop\Ideas\Ecommerece\env\lib\site-packages\django\utils\functional.py" in __get__
  37.         res = instance.__dict__[self.name] = self.func(instance)

File "C:\Users\MMaleka\Desktop\Ideas\Ecommerece\env\lib\site-packages\django\core\paginator.py" in num_pages
  97.         if self.count == 0 and not self.allow_empty_first_page:

File "C:\Users\MMaleka\Desktop\Ideas\Ecommerece\env\lib\site-packages\django\utils\functional.py" in __get__
  37.         res = instance.__dict__[self.name] = self.func(instance)

File "C:\Users\MMaleka\Desktop\Ideas\Ecommerece\env\lib\site-packages\django\core\paginator.py" in count
  92.             return len(self.object_list)

Exception Type: TypeError at /cellphone-holder/
Exception Value: object of type 'method' has no len()