我正在建立一个电子商务网站。 我有一个模型有两个类(类别和产品), 我最近在list_view和我的产品上添加了分页,但是当我点击类别类时,我收到错误。
from django.shortcuts import render, get_object_or_404 from .models
import Category, Product from cart.forms import CartAddProductForm
from django.contrib.auth.decorators import login_required from
django.core.paginator import Paginator, EmptyPage, PageNotAnInteger
from django.db.models import Q
def product_list(request, category_slug=None):
category = None
categories = Category.objects.all()
products = Product.objects.filter(available=True)
query = request.GET.get("q")
if query:
products = Product.objects.filter(Q(name__icontains=query) |
Q(description__icontains=query)).distinct()
paginator = Paginator(products, 10,)
page = request.GET.get('page', 2)
try:
products= paginator.page(page)
except PageNotAnInteger:
# If page is not an integer deliver the first page
products = paginator.page(1)
except EmptyPage:
# If page is out of range deliver last page of results
products = paginator.page(paginator.num_pages)
if category_slug:
category = get_object_or_404(Category, slug = category_slug)
products = products.filter(category=Category)
return render(request, 'shop/product/list.html', {'category':category, 'categories': categories, 'products': products, 'page': page})
@login_required def product_detail(request, id, slug):
product = get_object_or_404(Product,id=id,slug=slug,available=True)
cart_product_form = CartAddProductForm()
return render(request,'shop/product/detail.html', {'product': product, 'cart_product_form': cart_product_form})
当我点击产品时,它工作正常并且分页也有效但当我点击类别显示其他产品组时,它会显示以下错误:
Traceback:
File
"C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-
packages\django-2.0.2-py3.6.egg\django\core\handlers\exception.py"
in inner
35. response = get_response(request)
File
"C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-
packages\django-2.0.2-py3.6.egg\django\core\handlers\base.py"
in _get_response
128. response = self.process_exception_by_middleware(e,
request)
File
"C:\Users\user\AppData\Local\Programs\Python\Python36\lib\site-
packages\django-2.0.2-py3.6.egg\django\core\handlers\base.py"
in _get_response
126. response = wrapped_callback(request,
*callback_args, **callback_kwargs)
File "C:\Users\user\Documents\my webpages\socialapp\shop\views.py" in
product_list
30. products = products.filter(category=Category)
Exception Type: AttributeError at /shop/softwares-games/ Exception
**Value: 'Page' object has no attribute 'filter'**
答案 0 :(得分:0)
查看docs和您的代码:
products.filter(category=Category)
:您正在尝试过滤Page对象,而不是Product
的管理员(因为之前您正在做products= paginator.page(page)
),因此错误。Page.object_list
请注意,我没有在本地测试此代码,但这应该:
def product_list(request, category_slug=None):
category = None
categories = Category.objects.all()
products = Product.objects.filter(available=True)
query = request.GET.get("q")
if query:
products = products.filter(
Q(name__icontains=query) | Q(description__icontains=query)).distinct()
if category_slug:
# Re-use your category manager for the lookup
category = get_object_or_404(categories, slug=category_slug)
products = products.filter(category=Category)
paginator = Paginator(products, 10)
# I would start with page 1 and name this variable page_number
page_number = request.GET.get('page', 1)
try:
page = paginator.page(page_number)
except PageNotAnInteger:
# If page is not an integer deliver the first page
page = paginator.page(1)
except EmptyPage:
# If page is out of range deliver last page of results
page = paginator.page(paginator.num_pages)
return render(request, 'shop/product/list.html', {
'category': category, 'categories': categories,
'products': page.object_list, 'page': page})