45 lines
1.4 KiB
Python
45 lines
1.4 KiB
Python
from django.shortcuts import render
|
|
from django.views.generic import ListView, DetailView
|
|
from .models import Product
|
|
from django.contrib.auth import get_user_model
|
|
from django.shortcuts import get_object_or_404
|
|
from django.http import Http404
|
|
from carts.models import Cart
|
|
|
|
User = get_user_model()
|
|
|
|
class ProductListView(ListView):
|
|
queryset = Product.objects.all()
|
|
template_name = "products/list.html"
|
|
|
|
# def get_context_data(self, *args, **kwargs):
|
|
# context = super().get_context_data(*args, **kwargs)
|
|
# return context
|
|
|
|
|
|
class ProductDetailView(DetailView):
|
|
queryset = Product.objects.all()
|
|
template_name = "products/details.html"
|
|
|
|
def get_context_data(self, *args, **kwargs):
|
|
context = super().get_context_data(*args, **kwargs)
|
|
|
|
cart_obj, new_cart = Cart.objects.new_or_get(self.request)
|
|
context["cart"] = cart_obj
|
|
return context
|
|
|
|
def get_object(self, *args, **kwargs):
|
|
# request = self.request
|
|
slug = self.kwargs.get('slug')
|
|
inst = get_object_or_404(Product, slug=slug)
|
|
# if inst is None:
|
|
# raise Http404("Product doesn't exist")
|
|
# return inst
|
|
# try:
|
|
# inst = Product.objects.get(slug=slug)
|
|
#
|
|
# except ObjectDoesNotExist:
|
|
# raise Http404("not found...")
|
|
# except Product.MultipleObjectsReturned:
|
|
# inst = Product.objects.filter(slug=slug)[0]
|
|
return inst |