Answer the question
In order to leave comments, you need to log in
How to make a cart in django?
How to make a cart in django?
Or how to fix it to work?
there is a product model
models.py
#модель товара
class Ware(models.Model):
title = models.CharField(max_length = 130)
description = models.TextField()
prise = models.DecimalField(max_digits = 7, decimal_places=0)
available = models.BooleanField(default=True)
image = models.ImageField(upload_to='images/', blank=True)
avtor = models.CharField(max_length = 130)
def __str__(self):
return self.title
#модель корзины
class CartItem(models.Model):
ware = models.ForeignKey(
'Ware',
on_delete=models.CASCADE,
)
qty = models.PositiveIntegerField(default=1)
item_total = models.DecimalField(max_digits=9, decimal_places=0)
def __str__(self):
return "Cart item for product {0}".format(self.product.title)
class Cart(models.Model):
items = models.ManyToManyField(CartItem, blank=True)
cart_total = models.DecimalField(max_digits=9, decimal_places=0)
def __str__(self):
return str(self.id)
#это моя корзина
def cart_view(request):
cart = Cart.objects.first()
context ={
'cart': cart
}
return render(request, 'cart.html', context)
#добавление в корзину товара
def add_to_cart_view(request, ware_pk):
ware = Ware.objects.get(pk=ware_pk)
new_item, _ = CartItem.objects.get_or_create(ware=ware, item_ware=ware.prise)
cart = Cart.objects.first()
cart.items.add(new_item)
cart.save()
return HttpResponseRedirect("/")
#моя продукт
def testimage_add(request):
if request.method != 'POST':
form=CommentsForm()
else:
form=CommentsForm(request.POST, request.FILES)
if form.is_valid():
post = form.save(commit=False)
form.save()
return HttpResponseRedirect("/")
context={'form': form}
return render(request, 'ware.html', context)
urlpatterns = [
url('(?P<pk>[0-9]+)/', DetailView.as_view(model = Ware, template_name = 'detail_ware.html')),
url('add_to_cart/(?P<ware_pk>[-\w]+)/', views.add_to_cart_view, name='add_to_cart_view'),
url('cart/', views.cart_view, name='cart_view'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Answer the question
In order to leave comments, you need to log in
https://github.com/bmentges/django-cart
https://github.com/nevimov/django-easycart
And in general - look at https://djangopackages.org/ :-)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question