我正在使用Django和Wagtail CMS在一个样机电子商务网站上工作。我在将商品删除到购物车应用程序时遇到问题。
我正在使用POST方法,该方法会根据产品ID更新购物车。如果购物车中的对象:删除其他:添加,它可以作为切换。 它在产品页面上正常工作,但是当我尝试在购物车页面上删除时,我在/ cart / update /处收到ValueError 以10为底的int()的无效文字:'' 好像商品ID没有通过?
这是触发POST方法的按钮的HTML
<form method='POST' action='{% url "cart:update" %}' class="form"> {% csrf_token %}
<input type="hidden" name="product_id" value="{{ self.id }}" />
{% if incart %}
<button type="submit" class="btn btn-danger btn-md my-0 p btn-sm" type="submit">
Remove
</button>
{% else %}
<button type="submit" class="btn btn-primary btn-md my-0 p" type="submit">Add
to cart
<i class="fa fa-shopping-cart"></i>
</button>
{% endif %}
</form>
{{ self.id }}
变量来自产品页面模型。使用a页模型。
页面模型
class ProductDetailPage(Page):
def get_url(self):
return self.url
productImage = models.ForeignKey(
"wagtailimages.Image",
blank = False,
null = True,
on_delete = models.SET_NULL
)
productPrice = models.FloatField(
null = False,
blank = False,
default = 0
)
content_panels = Page.content_panels + [
ImageChooserPanel("productImage"),
FieldPanel("productPrice")
]
这是我的购物车视图
def cart_update(request):
product_id = request.POST.get('product_id')
if product_id is not None:
product_obj = Product.objects.get(id=product_id)
cart_obj, new_obj = Cart.objects.new_or_get(request)
if product_obj in cart_obj.products.all():
cart_obj.products.remove(product_obj)
else:
cart_obj.products.add(product_obj)
return redirect("cart:home")
答案 0 :(得分:0)
在HTML中,使用<input>
标记两种不同的方式:
{% if in_cart %}
<Button>…</Button>
<Input type='hidden' value='…' />
{% else %}
<Button>…</Button>
<Input type='hidden '…' />
{% endif %}`