尝试在python-django中订购时出现错误

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

每当我试图订购一些东西,我不断得到这个错误。

这是我的views.py:

if not request.user.is_authenticated:
    session = request.session
    cart = session.get(settings.CART_SESSION_ID)
    if cart:
        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
    del session[settings.CART_SESSION_ID]

return redirect('product_list')

请问有谁能帮我修一下吗?

3htmauhk

3htmauhk1#

您的会话中不存在该密钥,因此请在删除之前检查它是否存在

if settings.CART_SESSION_ID in session:
    del session[settings.CART_SESSION_ID]

相关问题