Python 在django中将特定数量的产品添加到购物车中

Python 在django中将特定数量的产品添加到购物车中,python,django,django-models,django-templates,django-views,Python,Django,Django Models,Django Templates,Django Views,我有一个问题,因为我不知道如何实现“向购物车添加特定数量的产品”。 目前,在按下“添加到购物车”按钮后,将添加单个数量的产品 我不知道如何在视图和模板中与此相关 我希望它采用给定的值,而不是1 cart_item.quantity += 1 购物车/视图.py def add_cart(request, product_id): product = Product.objects.get(id=product_id) try: cart = Cart.objec

我有一个问题,因为我不知道如何实现“向购物车添加特定数量的产品”。 目前,在按下“添加到购物车”按钮后,将添加单个数量的产品

我不知道如何在视图和模板中与此相关

我希望它采用给定的值,而不是1

cart_item.quantity += 1
购物车/视图.py

def add_cart(request, product_id):
    product = Product.objects.get(id=product_id)
    try:
        cart = Cart.objects.get(cart_id=_cart_id(request))
    except Cart.DoesNotExist:
        cart = Cart.objects.create(
                cart_id = _cart_id(request)
            )
        cart.save()
    try:
      cart_item = CartItem.objects.get(product=product, cart=cart)
      if cart_item.quantity < cart_item.product.stock:
          cart_item.quantity += 1
      cart_item.save()
    except CartItem.DoesNotExist:
      cart_item = CartItem.objects.create(
                  product = product,
                  quantity = 1,
                  cart = cart
          )
      cart_item.save()
    return redirect('cart:cart_detail')
class Cart(models.Model):
    cart_id = models.CharField(max_length=250, blank=True)
    date_added = models.DateField(auto_now_add=True)
    class Meta:
        db_table = 'Cart'
        ordering = ['date_added']

    def __str__(self):
        return self.cart_id

class CartItem(models.Model):
    product = models.ForeignKey(Product, on_delete=models.CASCADE)
    cart = models.ForeignKey(Cart, on_delete=models.CASCADE)
    quantity = models.IntegerField()
    active = models.BooleanField(default=True)
    class Meta:
        db_table = 'CartItem'

    def sub_total(self):
        return self.product.price * self.quantity

    def __str__(self):
        return self.product
模板/shop/product.html

  <div class="float-right">
   <div class="float-left"> <input type="number" value="1" aria-label="Search" 
    class="form-control" style="width: 100px"> </div>
    <a class="btn send-click btn-md my-0 p"  href="{% url 'cart:add_cart' product.id %}">Add to Cart</a></div>

url.py

app_name='cart'

urlpatterns = [
    path('add/<int:product_id>/', views.add_cart, name='add_cart'),
    path('', views.cart_detail, name='cart_detail'),
    path('remove/<int:product_id>/', views.cart_remove, name='cart_remove'),
    path('full_remove/<int:product_id>/', views.full_remove, name='full_remove'),
]
app\u name='cart'
URL模式=[
路径('add/',views.add_cart,name='add_cart'),
路径(“”,views.cart\u detail,name='cart\u detail'),
路径('remove/',views.cart\u remove,name='cart\u remove'),
路径('full_remove/',views.full_remove,name='full_remove'),
]

如果您不想更改为具有post处理的表单,可以使用内嵌JS获取金额。您需要调整您的url和视图以同时接受项目的数量

<div class="float-right">
   <div class="float-left"> <input id="amount_field" type="number" value="1" aria-label="Search" 
    class="form-control" style="width: 100px"> </div>
    <button type="button" onclick="location.href='{% url \"cart:add_cart\" product.id %}'.concat("/",document.getElementById('amount_field').value);" >Add Items</button>
</div>

添加项目

我在上面添加了url.py。但是,我仍然不明白,在CartItem模型中,我必须创建“金额\字段”?您如何使用数量字段?如果我理解正确,我必须创建列,例如quantityAdd,而不是“1”。我正在将数量分配给quantityAdd,对吗?否输入字段应该包含您要添加的编号。我在quantityAdd模型中创建,并尝试添加name=“quantityAdd”并将“quantityAdd”分配给视图中的cart\u item.quantity。