def product_list(request, category_slug=None):
category = None
categories = Category.objects.all()
products = Product.objects.filter(available=True)
page = request.GET.get('page')
paginator = Paginator(products, 3)
if category_slug:
category = get_object_or_404(Category, slug=category_slug)
products = products.filter(category=category)
try:
products = paginator.page(page)
except PageNotAnInteger:
products = paginator.page(1)
except EmptyPage:
products = paginator.page(paginator.num_pages)
return render(request,
'products/list.html',
{'category': category,
'categories': categories,
'products': products,
'page': page})