Issue
I want to compare the received price with the previous prices and send a message if it was lower, but it shows this error to receive the first entry. I also tried to try and while. It didn't give the expected result.
If I remove this condition and enter a number and apply this condition again. The code works correctly.
IndexError at /bid/2/
list index out of range
File "C:\Users\N\Desktop\my own\views.py", line 98, in bid
elif bid_price <= other_off[0].bid_price :
def bid(request, bidid):
bid_price = Decimal(request.POST.get('bid_price', False))
other_off =
Bid_info.objects.filter(product=product).order_by('-
bid_price')
if Bid_info.objects.filter(product=product,
seller=request.user).exists():
messages.warning(request, "You've already made an offer
for this product.")
elif bid_price <= other_off[0].bid_price :
messages.warning(request, "Your offer must be greater
than other offers.")
else:
Bid_ = Bid_info(product=product, seller=request.user, bid_price=bid_price)
Solution
There are no Bid
s for that Product
, hence other_off[0]
will not work. You can however first check if there is a bit, and do this all in the same query with:
def bid(request, bidid):
bid_price = Decimal(request.POST.get('bid_price', False))
other_off = (
Bid_info.objects.filter(product=product).order_by('-bid_price').first()
)
if Bid_info.objects.filter(product=product, seller=request.user).exists():
messages.warning(
request, "You've already made an offer for this product."
)
elif other_off and bid_price <= other_off.bid_price:
messages.warning(request, "Your offer must be greater than other offers.")
else:
Bid_ = Bid_info.objects.create(
product=product, seller=request.user, bid_price=bid_price
)
Answered By - willeM_ Van Onsem
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.