Files
shoppy/web/views.py
T
2024-11-26 13:20:54 +01:00

51 lines
1.3 KiB
Python

from django.shortcuts import get_object_or_404
from django.views.generic import TemplateView
from shop.filters import ProductFilter
from shop.models import Product
from web.mixins import FilteredQuerysetMixin, PaginatedQuerysetMixin
class IndexView(TemplateView):
template_name = "web/index.html"
def get_context_data(self, **kwargs):
return {
"title": "Shoppy",
}
class ListProducts(TemplateView, FilteredQuerysetMixin, PaginatedQuerysetMixin):
template_name = "web/list_products.html"
queryset = Product.objects.all()
filter_class = ProductFilter
def get_context_data(self, **kwargs):
qs = self.get_queryset()
page = self.get_paginated_queryset(qs)
return {
"page": page,
"has_next_page": page.has_next(),
"has_previous_page": page.has_previous(),
}
class ProductDetail(TemplateView):
template_name = "web/product_detail.html"
def get_context_data(self, pk, slug, **kwargs):
product = get_object_or_404(Product, pk=pk)
return {
"product": product,
"title": product.name,
"description": product.description,
"image": product.images.first(),
}
index = IndexView.as_view()
list_products = ListProducts.as_view()
product_detail = ProductDetail.as_view()