40 lines
1.5 KiB
Python
40 lines
1.5 KiB
Python
from django.shortcuts import render, get_object_or_404
|
|
from django.core.paginator import Paginator
|
|
from .models import ProductCategory, Product
|
|
|
|
|
|
def product_list(request):
|
|
category_slug = request.GET.get('category')
|
|
categories = ProductCategory.objects.filter(is_active=True)
|
|
products = Product.objects.filter(is_active=True)
|
|
if category_slug:
|
|
cat = get_object_or_404(ProductCategory, slug=category_slug, is_active=True)
|
|
products = products.filter(category=cat)
|
|
paginator = Paginator(products, 12)
|
|
page_obj = paginator.get_page(request.GET.get('page'))
|
|
breadcrumb_items = [{'name': '产品中心', 'url': ''}]
|
|
return render(request, 'products/list.html', {
|
|
'categories': categories,
|
|
'page_obj': page_obj,
|
|
'is_paginated': page_obj.has_other_pages(),
|
|
'current_category': category_slug,
|
|
'breadcrumb_items': breadcrumb_items,
|
|
})
|
|
|
|
|
|
def product_detail(request, slug):
|
|
product = get_object_or_404(Product, slug=slug, is_active=True)
|
|
gallery = product.images.all()
|
|
related = Product.objects.filter(category=product.category, is_active=True).exclude(pk=product.pk)[:4]
|
|
breadcrumb_items = [
|
|
{'name': '产品中心', 'url': '/products/'},
|
|
{'name': product.category.name_zh, 'url': ''},
|
|
{'name': product.name_zh, 'url': ''},
|
|
]
|
|
return render(request, 'products/detail.html', {
|
|
'product': product,
|
|
'gallery': gallery,
|
|
'related': related,
|
|
'breadcrumb_items': breadcrumb_items,
|
|
})
|