如何解决如何在Django中从“收藏夹”中删除产品?
我在Django数据库中有一个愿望清单表,并且customer
与愿望清单表相关,这意味着如果客户登录了,那么他/她可以在wishlist
中添加产品,但是我试图从客户的愿望清单中删除产品,但是它正在重定向,请让我知道如何从客户的愿望清单中删除产品。
这是我的models.py
文件...
class Wishlist(models.Model):
product = models.ForeignKey(Product,on_delete=models.CASCADE,default=None)
customer = models.ForeignKey(Customer,on_delete=models.SET_NULL,blank=True,null=True)
quantity = models.IntegerField(default=0,null=True,blank=True)
def __str__(self):
return str(self.id)
这是我的views.py
文件...
def deletewishlist(request,id):
customer=request.user.customer
Wishlist.objects.filter(customer_id=customer.id,id=id).delete()
messages.success(request,'Product Remove From Wishlist...')
return HttpResponseRedirect('/wishlist')
这是我的urls.py
文件...
path('wishlist_item/deleteproduct/<int:id>',views.deletewishlist,name="deletewishlist"),
这是我的delete
按钮代码,单击该按钮代码即可从愿望清单中删除产品。
<a class="primary-btn" href="/wishlist_item/deleteproduct/{{item.product.id}}" onclick="return confirm('Are you sure')">Delete</a>
解决方法
您正在查询Wishlist
ID,而不是product
ID
使用:
def deletewishlist(request,id):
customer=request.user.customer
Wishlist.objects.filter(customer_id=customer.id,product=Product.objects.get(id=id)).delete()
messages.success(request,'Product Remove From Wishlist...')
return HttpResponseRedirect('/wishlist')
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。