在python-django订购后,该产品不会从购物车中删除

6tr1vspr  于 2023-03-20  发布在  Go
关注(0)|答案(1)|浏览(130)

我试图使一个电子商务网站。有1个问题,一直很难解决。问题:订购后,产品应该从购物车中删除。但它不起作用。没有语法错误,但订购后仍有产品剩余。
下面是我的www.example.com的一部分views.py:

if not request.user.is_authenticated:
    session = request.session
    cart = session.get(settings.CART_SESSION_ID)
    if cart is not None:
        del session[settings.CART_SESSION_ID]
else:
    customer = request.user.customer
    order, created = Order.objects.get_or_create(
        customer=customer, complete=False)

    order_products = OrderProduct.objects.filter(order=order)
    if order_products:
        order_product = order_products[0]
    else:
        order_product = OrderProduct.objects.create(order=order)

    order.save()
    messages.success(request, 'Заказ успешно оформлен. Проверьте свою электронную почту!!!')
    session = request.session
    cart = session.get(settings.CART_SESSION_ID)
    if cart is not None:
        del session[settings.CART_SESSION_ID]
        session.modified = True

return redirect('product_list')

我真的希望有人能帮我,拜托。

iqjalb3h

iqjalb3h1#

你的主要问题是,你检查购物车是否是空的或不后,您作出了订单,因为我理解messages.success
实际上并不需要if cart is not None:
像这样使用它:

order.save()
messages.success(request, 'Заказ успешно оформлен. Проверьте свою электронную почту!!!')
session = request.session
cart = session.get(settings.CART_SESSION_ID)
del session[settings.CART_SESSION_ID]
session.modified = True

或将其更改为:

cart = session.get(settings.CART_SESSION_ID)
del cart[settings.CART_SESSION_ID]
cart.modified = True

相关问题