python - 如何从 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')
推荐阅读
- c++ - 如何确定时间/空间复杂度
- reactjs - 从 API 获取数据时如何解决“CORS 策略已阻止访问 XMLHttpRequest”
- ios - iOS 模拟器 - 录制模拟器屏幕并包含设备框架?
- angular - Chrome 中的 Angular Universal 不呈现服务器内容(查看源代码)
- angular - 我们如何在 Docker (Windows 10) 中为 ASP.NET Core 应用程序运行 NodeJ?
- python - 如何使用 kivy 轮播在一页上有两个小部件?
- python - 如何将边界框合并为一个
- robocopy - Robocopy:在执行前检查文件是否存在
- javascript - xhr.onreadystatechange 永远不会被调用
- javascript - 通过在文本字段中输入数字来切换 CSS 样式条件(Javascript?)