在尝试在Django中创建订单时出现错误。

huangapple go评论76阅读模式
英文:

There is error when trying to make order in django

问题

当我尝试订购东西时,一直出现这个错误:

在尝试在Django中创建订单时出现错误。

这是我的views.py文件:

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

    order_product, created = OrderProduct.objects.get_or_create(
        order=order, )
    order.save()
messages.success(request, 'Заказ успешно оформлен. Проверьте свою электронную почту!!!')
return redirect('product_list')

请问有谁能帮助我修复这个错误?

英文:

When I try to order something, I keep getting this error:

在尝试在Django中创建订单时出现错误。

Here is my views.py

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

    order_product, created = OrderProduct.objects.get_or_create(
        order=order, )
    order.save()
messages.success(request, 'Заказ успешно оформлен. Проверьте свою электронную почту!!!')
return redirect('product_list')

Please can anyone help me to fix this error?

答案1

得分: 0

以下是翻译好的部分:

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:
        # Remove cart items from the session
        del session[settings.CART_SESSION_ID]
        session.modified = True

return redirect('product_list')

注意:上述代码中包含了一个俄语文本 'Заказ успешно оформлен. Проверьте свою электронную почту!!!',没有提供翻译,保留原文。

英文:
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:
        # Remove cart items from the session
        del session[settings.CART_SESSION_ID]
        session.modified = True
  
return redirect('product_list')

huangapple
  • 本文由 发表于 2023年3月9日 17:52:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/75682903.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定