Я внес эту правку, и она сработала. views.py
class ItemDetailView(LoginRequiredMixin, DetailView):
model = Item
template_name = "product.html"
def get(self, *args, **kwargs):
url = self.request.get_full_path()
lst = url.split("/")
slug = lst[-2]
item = Item.objects.get(slug=slug)
items = Item.objects.filter(category=item.category)[0:4]
items1 = Item.objects.filter(category=item.category)[5:9]
reviews = Reviews.objects.filter(item=item)
context={'object':item,'object_list':items,'object_list1':items1, 'reviewsget':reviews}
if len(reviews)<1:
context['comment']="Be first to comment..."
else:
context['comment']=''
return render(self.request,self.template_name,context=context)
def post(self, *args, **kwargs):
if self.request.method == 'POST':
#item = get_object_or_404(Item, slug=slug)
url = self.request.get_full_path()
lst = url.split("/")
slug = lst[-2]
item = Item.objects.get(slug=slug)
item.selcsize = self.request.POST.get("sizes_choice")
item.save()
try:
rating = self.request.POST.get("rating")
text = self.request.POST.get("text")
try:
images = self.request.FILES.get("imgs")
except:
pass
review = Reviews.objects.create(
user = self.request.user,
item = item,
review=text,)
try:
review.images = images
review.rating = str(rating)
review.save()
except:
pass
except:
pass
if self.request.POST.get("sizes_choice")!=None:
messages.info(self.request, "This item is selected with size "+str(item.selcsize))
items = Item.objects.filter(category=item.category)[0:4]
items1 = Item.objects.filter(category=item.category)[5:9]
reviews = Reviews.objects.filter(item=item)
context={'object':item,'object_list':items,'object_list1':items1, 'reviewsget':reviews}
if len(reviews)<1:
context['comment']="Be first to comment..."
else:
context['comment']=''
return render(self.request,self.template_name,context=context)
models.py
class Sizes_class(models.Model):
sizes_choice = ArrayField(models.CharField(null=True, blank=True,max_length = 5), blank=True, default=list)
class Item(models.Model):
title = models.CharField(max_length=100)
price = models.FloatField()
tag = models.CharField(max_length=10,blank=True,null=True,default='New')
discount_price = models.FloatField(blank=True, null=True)
dis_per = models.FloatField(blank=True, null=True,default=-1)
category = models.CharField(choices=CATEGORY_CHOICES, max_length=2)
label = models.CharField(choices=LABEL_CHOICES, max_length=1)
slug = models.SlugField()
description = models.TextField()
image = models.ImageField()
weight = models.FloatField(default=0.5)
pincode = models.IntegerField(default=123456)
schargeinc = models.FloatField(blank=True, null=True,default=-1)
has_size = models.BooleanField(default=False,blank=True, null=True,)
size = models.ForeignKey(Sizes_class, on_delete=models.CASCADE, null=True, default=False)
selcsize = models.CharField(default=False,null=True,blank=True,max_length=25)
def __str__(self):
return self.title
def get_add_cart_wsize_url(self):
return reverse("core:size", kwargs={
'slug': self.slug
})
def get_absolute_url(self):
return reverse("core:product", kwargs={
'slug': self.slug
})
def get_add_to_cart_url(self):
return reverse("core:add-to-cart", kwargs={
'slug': self.slug
})
def get_remove_from_cart_url(self):
return reverse("core:remove-from-cart", kwargs={
'slug': self.slug
})
И зарегистрируйте эти модели в admin.py