模板中的Django POST请求(无格式或更好的解决方案)

时间:2019-10-22 08:14:39

标签: python html css django templates

我有一个购物车,我想向该购物车中添加一个对象。我按下了一个按钮,当发生这种情况时,我希望请求能够通过。

这是我要在POST请求中执行的操作(按下按钮时):

  1. 检查与我按下的产品具有相同ID的商品。
  2. 使用与我上面检查过的商品相同的商品创建一个CartItem。
  3. 将该购物车项目添加到链接到我的个人资料的购物车中。 (尚未开始工作)

型号:

class Item(models.Model):
name = models.CharField(max_length=100, null=True)
info = models.CharField(max_length=100, null=True)
price = models.IntegerField(default=0, null=True)

discount = models.CharField(max_length=100, null=True, blank=True)
discountPrice = models.IntegerField(default=0, null=True)

inStock = models.BooleanField(default=False)
imagefield = models.ImageField()
reviews = models.ManyToManyField(Review, blank=True, related_name="itemreview")

class Meta:
    verbose_name_plural = 'Items'

def __str__(self):
    return "{name}".format(name=self.name)




class CartItem(models.Model):
item = models.ForeignKey(Item, blank=True, related_name="CartItem", on_delete=models.CASCADE)
quantity = models.IntegerField(default=0, null=True)
price = models.IntegerField(default=0, null=True)

class Meta:
    verbose_name_plural = 'Cart Items'

def __str__(self):
    return self.item.name

def get_total_item_price(self):
    return self.quantity * self.item.price




class ShoppingCart(models.Model):
user = models.OneToOneField(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
items = models.ManyToManyField(CartItem, blank=True, related_name="orderitem")

class Meta:
    verbose_name_plural = 'Shopping Carts'

def __str__(self):
    return self.user.username

def get_total(self):
    total = 0
    for cart_item in self.items.all():
        total += cart_item.get_total_item_price()
    return total


@receiver(post_save, sender=User)
def create_user_data(sender, update_fields, created, instance, **kwargs):
if created:
    user = instance
    profile = ShoppingCart.objects.create(user=user)

查看和网址:

def add_to_cart(request, item_id):
if request.method == 'POST':

    #create a cartitem
    selectedItem = Item.objects.filter(id=item_id)
    theQuantity = 1
    thePrice = selectedItem.price
    cartItem = CartItem(quantity=theQuantity, price=thePrice)
    cartItem.save()
    cartItem.item.add(selectedItem)

    #add that cartitem to the cart

    return redirect('landing-page')



urlpatterns = [
  path('', views.landingPage, name='landing-page'),
  path('product/<int:itemID>', views.singleProduct, name='singleProduct'),
  path('cart', views.shoppingCart, name='cart'),

  path('add-to-cart/<int:item_id>', views.add_to_cart, name="addToCart"),
  path('quantity-up/<int:cartitem_id>', views.change_quantity_up, name="quantity-upp"),
  path('quantity-down/<int:cartitem_id>', views.change_quantity_down, name="quantity-down"),
]

这是我发出请求的方式,并且在html内是带有href =“ {%url'addToCart'item.id%}”的按钮

https://ci.apache.org/projects/flink/flink-docs-stable/monitoring/rest_api.html#jobs-jobid-checkpoints

html:

    <a class="button primary-btn" href="{% url 'addToCart' item.id %}">Add to Cart</a> 

0 个答案:

没有答案