django 自动移动到购物车功能

am46iovg  于 2023-11-20  发布在  Go
关注(0)|答案(1)|浏览(83)

我们试图找出如何将拍卖项目的车。我们有我们的计划从拍卖列表页面中删除拍卖,但我们还没有得到它移动到最高投标人车。
下面是我们的auction_details函数,mycart函数auctions_details

def auction_details(request, auction_id):
    auction = get_object_or_404(Auction, id=auction_id)
 
    if request.method == 'POST':
        bid_form = BidForm(request.POST)
 
        if bid_form.is_valid():
            bid_amount = bid_form.cleaned_data['bid_amount']
 
            if bid_amount > auction.current_bid:
                # Update the auction details
                auction.current_bid = bid_amount
                auction.highest_bidder = request.user
                Bid.objects.create(bidder=request.user, auction=auction, amount=bid_amount)
                messages.success(request, f'Your bid successfully is placed at {timezone.now()}!')
                messages.success(request, f'The auction will expire at {auction.end_date}!')
                messages.success(request, f'Expired?  {auction.end_date <= timezone.now()}!')
            
                if auction.end_date <= timezone.now():
                    auction.ended = True
                
                    cart_item, created = CartItem.objects.get_or_create(user=auction.highest_bidder, auction=auction)
                    cart_item.quantity = 1
                    cart_item.save()

                auction.save()        
                
                return redirect('auction_details', auction_id=auction.id)
 
            else:
                messages.error(request, 'Bid must be higher than the current bid.')

    else:
        bid_form = BidForm()

 
    return render(request, 'auctions/auction_details.html', {'auction': auction, 'bid_form': bid_form})

mycart fuction - 
def mycart(request):
    if request.user.is_authenticated:
        cart_items = CartItem.objects.filter(user=request.user)
    else:
        cart_items = []

    total_amount = 0  # Initialize total amount to zero

    for item in cart_items:
        total_amount += item.auction.current_bid * item.quantity

    context = {
        'cart_items': cart_items,
        'total_amount': total_amount,  # Pass the total_amount to the template
    }

    return render(request, 'auctions/mycart.html', context)

字符串
我们认为这里的代码行有问题:

if auction.end_date <= timezone.now():
                    auction.ended = True


在后端,当拍卖结束时,它不会将其标记为结束如果您需要任何其他信息,请让我知道
我们试过

if auction.end_date <= timezone.now():
                    auction.ended = True


移除它,改变它,但似乎都不起作用

dohp0rv5

dohp0rv51#

我尝试将if-else语句重新组织为guard clauses,以更好地识别其背后的逻辑:

def auction_details(request, auction_id):
    auction = get_object_or_404(Auction, id=auction_id)

    if request.method != 'POST':
        return render(request, 'auctions/auction_details.html', {'auction': auction, 'bid_form': BidForm()})

    if not (bid_form := BidForm(request.POST)).is_valid():
        messages.error(request, 'Bid must be higher than the current bid.')
        return render(request, 'auctions/auction_details.html', {'auction': auction, 'bid_form': bid_form})

    if (bid_amount := bid_form.cleaned_data['bid_amount']) <= auction.current_bid:
        return render(request, 'auctions/auction_details.html', {'auction': auction, 'bid_form': bid_form})
    
    # Update the auction details
    auction.current_bid = bid_amount
    auction.highest_bidder = request.user
    Bid.objects.create(bidder=request.user, auction=auction, amount=bid_amount)
    messages.success(request, f'Your bid successfully is placed at {timezone.now()}!')
    messages.success(request, f'The auction will expire at {auction.end_date}!')
    messages.success(request, f'Expired?  {auction.end_date <= timezone.now()}!')

    if auction.end_date <= timezone.now():
        auction.ended = True

        cart_item, created = CartItem.objects.get_or_create(
            user=auction.highest_bidder, auction=auction)
        cart_item.quantity = 1
        cart_item.save()

    auction.save()

    return redirect('auction_details', auction_id=auction.id)

字符串
由此看来,if auction.end_date <= timezone.now():部分只有在拍卖到期后进行有效出价时才会执行。
解决这个问题的一个简单方法是将部件放在函数的开头:

def auction_details(request, auction_id):
    auction = get_object_or_404(Auction, id=auction_id)

    if auction.end_date <= timezone.now():
        auction.ended = True

        cart_item, created = CartItem.objects.get_or_create(
            user=auction.highest_bidder, auction=auction)
        cart_item.quantity = 1
        cart_item.save()
        auction.save()
        render(request, 'auctions/auction_details.html', {'auction': auction, 'bid_form': BidForm()})

    # [...] rest of code

相关问题